Classes

Python, as an object oriented programming language is most useful with its properties and methods.
And class is kind of an object constructor. Writing own classes means, that you reach out a high level of programming.
Let’s start with really simple example to get the idea.

#Creating class
class PersonalData:
    PName = "Agnieszka"
    PAge = "31"
    PSex = "woman" #declare it attribues

#a sample method
    def MeetMe(self):
        print("Hi! My name is", self.PName)
        print("I'm", self.PAge,"years old")
        print("and I am a", self.PSex)

Let’s check what we’ve got:

#object instantiation
Sentence = PersonalData()
Sentence.MeetMe()
Result

Another example with _init_ parameter:

class Moose:
    WildAnimal = 'moose'

    def __init__(self, color, sex, age):
        self.color = color
        self.sex = sex
        self.age = age

MareMoose = Moose("Dark brown with white socks","female","Adult")
HerChild = Moose("Brown", "male", "3 months")

print('Mare  parameters')
print('Color:', MareMoose.color)
print('Sex:', MareMoose.sex)
print('Age:', MareMoose.age)

print('Child parameters')
print('Color:', HerChild.color)
print('Sex:', HerChild.sex)
print('Age:', HerChild.age)

print("Mare in the uploaded movie is a", Moose.WildAnimal)
results

Analysing the example above most of us might think “what for I need classes if i have to declare something twice to use it? Time for example that will let us decide about the ressult of used parameters. Le’ts try again with one above:

class Moose:
    WildAnimal = 'moose'

    def __init__(self, name):
        self.name = name

    def SetColor(self,color):
        self.color = color

    def GetColor(self):
        return self.color

FemaleMoose = Moose("Mare")
FemaleMoose.SetColor('Dark brown with white socks')
print(FemaleMoose.name, "color:",FemaleMoose.GetColor())