Create a class:
To define a class or a class in Python, you can use the keyword for class, followed by the name class and a colon.
Within the class, the __init__ method must be defined with def.
This is the initializer you can use later to create objects, and it's similar to the builder in Java.
Method __init__ must always be there! It takes one parameter: self, which refers to the object itself.
Within the method, the keyword pass is used as of now, because Python expects you to write something there.
Remember to always use the correct indentation!
class Cat:
def __init __ (self): pass
Note: self in Python is equivalent to this in C ++ or Java.
In our case this you have the cat class (mostly empty), and there isn't any object yet, let's create one!
Instantiating objects:
To create an object, type the class name, followed by two parentheses, and you can base this on a variable to track the object.
ozzy = Cat ()
We print it:
print (ozzy)
Adding features to the class:
After printing ozzy, it is clear that this object is a cat, but you haven't added any attributes yet.
Let's give the dog category a name and an age, by rewriting it:
class Cat:
def __init __ (self, name, age):
self.name = name
self.age = age
You can see that the function now takes parameters after self: name and age, and then assigns them to self.name and self.age respectively.
You can now create a new ozzy object, with a name and age:
ozzy = Cat ("Ozzy", 2)
To access attributes of an object in Python, you can use point notation. This is done by typing the object name, followed by a period and the attribute name.
print (ozzy.name)
print (ozzy.age)
Ozzy 2
This can also be combined into a more detailed sentence:
print (ozzy.name + "is" + str (ozzy.age) + "year (s) old.")
Ozzy is 2 year (s) old.
The str () function is used here to convert the age attribute, which is an integer, into a string, so that you can use it in the print () function.
Definition of methods or methods within the class:
Now that you've got the cat class, he has a name and age that you can track, but he's not actually doing anything.
This is where the alternate methods come from, as you can rewrite the category to now include the meow method.
Notice how the def keyword is used again, as well as the self argument.
class Cat:
def __init __ (self, name, age):
self.name = name
self.age = age
def meow (self):
print ("meow meow!")
The meowing method can now be called using bullet notation, after creating a new harmonious object.
The "meow meow!" Should be printed. on the screen.
Notice the parentheses (curly braces) in meow (). These are always used when calling a method.
It is empty in this case, since the meow method does not take any parameters.
ozzy = Cat ("Ozzy", 2)
ozzy.meow ()
meow meow!
Remember how you printed ozzy earlier?
The code below now implements this function in Class Cat, using the catinfo () method.
Then create some objects with different properties, and call a method on them.
class Cat:
def __init __ (self, name, age):
self.name = name
self.age = age
def meow (self):
print ("meow meow!")
def catinfo (self):
print (self.name + "is" + str (self.age) + "year (s) old.")
ozzy = Cat ("Ozzy", 2)
skippy = Cat ("Skippy", 12)
filou = Cat ("Filou", 8)
ozzy.catinfo ()
skippy.catinfo ()
filou.catinfo ()
Ozzy is 2 year (s) old.
Skippy is 12 year (s) old.
Filou is 8 year (s) old.
As you can see here, you can call the catinfo () method on objects using point notation.
Response now depends on the Cat object on which you are invoking the method.
Since cats are getting older, it would be nice if you could adjust their ages accordingly.
Ozzy turns 3, so let's change his age.
ozzy.age = 3print (ozzy.age)
>>> 3
It's as easy as setting a new value for the attribute, you can also implement this as a birthday method in the cat category:
class Cat:
def __init __ (self, name, age):
self.name = name
self.age = age
def meow (self):
print ("meow meow!")
def catinfo (self):
print (self.name + "is" + str (self.age) + "year (s) old.")
def birthday (self):
self.age + = 1
ozzy = Cat ("Ozzy", 2)
print (ozzy.age)
2
ozzy.birthday ()
print (ozzy.age)
3
Now you do not need to manually change the age of the cat, whenever it is his birthday, you can only summon the birthday method ().
Pass parameters to methodes:
Do you want cats to have a friend? This should be optional, given that not all dogs love to socialize.
Take a look at setBuddy () method below.
Take self, as usual, and buddy (friend) parameters.
In this case, buddy will be another Cat sprite.
We set self.buddy attribute to buddy, buddy.buddy attribute to self.
This means that the relationship is mutual, you are your friend's companion.
In this case, Filou will be Ozzy's friend, which means Ozzy automatically becomes Filou's friend.
You can also set these attributes manually, instead of defining a method, but that takes a lot of work (writing two lines of code instead of 1) every time you want to assign a friend.
Note that in Python, you don't need to specify the type of parameter or parameter, whereas if you are working with Java, it will be required.
class Cat:
def __init __ (self, name, age):
self.name = name
self.age = age
def meow (self):
print ("meow meow!")
def catinfo (self):
print (self.name + "is" + str (self.age) + "year (s) old.")
def birthday (self):
self.age + = 1
def setBuddy (self, buddy):
self.buddy = buddy
buddy.buddy = self
You can now summon the method using the period, and pass it as another Cat object.
In this case, it will be Filou Ozzy's friend:
ozzy = Cat ("Ozzy", 2)
filou = Cat ("Filou", 8)
ozzy.setBuddy (filou)
Methods of friends can also be called.
The self parameter passed as an argument to catinfo () is now ozzy.buddy, which is filou.
ozzy.buddy.catinfo ()
Filou is 8 year (s) old.