In Python, you define a class by using the class keyword followed by a name and a colon. Then you use .__init__() to declare which attributes each instance of the class should have:
# dog.py
class Dog:
def __init__(self, name, age):
self.name = name
self.age = ageIn the body of .__init__(), there are two statements using the self variable:
self.name = namecreates an attribute callednameand assigns the value of thenameparameter to it.self.age = agecreates an attribute calledageand assigns the value of theageparameter to it.
To instantiate this Dog class, you need to provide values for name and age. If you don’t, then Python raises a TypeError:
>>> Dog()
Traceback (most recent call last):
...
TypeError: __init__() missing 2 required positional arguments: 'name' and 'age'To pass arguments to the name and age parameters, put values into the parentheses after the class name:
>>> miles = Dog("Miles", 4)
>>> buddy = Dog("Buddy", 9)When you instantiate the Dog class, Python creates a new instance of Dog and passes it to the first parameter of .__init__(). This essentially removes the self parameter, so you only need to worry about the name and age parameters.
What is the use of self in Python
When working with classes in Python, the term “self” refers to the instance of the class that is currently being used. It is customary to use “self” as the first parameter in instance methods of a class. Whenever you call a method of an object created from a class, the object is automatically passed as the first argument using the “self” parameter. This enables you to modify the object’s properties and execute tasks unique to that particular instance.
The __init()___ is similar to constructors in C++ or JAVA. When you instantiate the Dog class, Python creates a new instance of Dog and passes it to the first parameter of .__init__(). This essentially removes the self parameter, so you only need to worry about the name and age parameters.
Instance methods are functions that you define inside a class and can only call on an instance of that class. Just like .__init__(), an instance method always takes self as its first parameter.
# dog.py
class Dog:
species = "Canis familiaris"
def __init__(self, name, age):
self.name = name
self.age = age
# Instance method
def description(self):
return f"{self.name} is {self.age} years old"
# Another instance method
def speak(self, sound):
return f"{self.name} says {sound}"Creating object and calling the methods
>>> miles = Dog("Miles", 4)
>>> miles.description()
'Miles is 4 years old'
>>> miles.speak("Woof Woof")
'Miles says Woof Woof'
>>> miles.speak("Bow Wow")
'Miles says Bow Wow'Inheritance
# dog.py
# ...
class JackRussellTerrier(Dog):
def speak(self, sound="Arf"):
return f"{self.name} says {sound}"
# ...
>>> miles = JackRussellTerrier("Miles", 4)
>>> miles.speak()
'Miles says Arf'
# dog.py
# ...
class JackRussellTerrier(Dog):
def speak(self, sound="Arf"):
return f"{self.name} says {sound}"
# ... # dog.py
# ...
class JackRussellTerrier(Dog):
def speak(self, sound="Arf"):
return super().speak(sound)
# ...super().speak(sound) inside JackRussellTerrier, Python searches the parent class, Dog, for a .speak() method and calls it with the variable sound.Garbage Collection in Python