Python Intermediate Classes
Python Intermediate Classes
This page covers Python intermediate classes and continues the previous tutorial of Python classes.
For Python classes here.
It encompasses some of the aspects of classes in more details.
- object modification
- “pass” statement
- __str()__ built-in function
Object modification
Modification within classes refers to the processes of amending and deleting of objects.
Changing the value of an object’s property happens through declaring a new value to the specific property.
Below is an example from previous Python classes tutorial.
# Class class Car: def __init__ (self, model, colour, n_doors): self.model = model self.colour = colour self.n_doors = n_doors # Objects car1 = Car("XYZ Ultimate", "Blue", 4) car2 = Car("ABV Extreme", "Yellow", 2)
Modify an object
The following modification changes the value of the property “colour”, from blue to red in the first object of “car1”.
class Car: def __init__ (self, model, colour, n_doors): self.model = model self.colour = colour self.n_doors = n_doors car1 = Car("XYZ Ultimate", "Blue", 4) car2 = Car("ABV Extreme", "Yellow", 2) # change of property value car1.colour = "Red" print(car1.colour) # Output: Red
Delete an object
The keyword “del” allows Python to delete either object properties or entire objects.
The following are two examples covering both deletion processes.
class Car: def __init__ (self, model, colour, n_doors): self.model = model self.colour = colour self.n_doors = n_doors car1 = Car("XYZ Ultimate", "Blue", 4) car2 = Car("ABV Extreme", "Yellow", 2) # deleting an object's property del car1.colour print(car1.colour) # Output: AttributeError: 'Car' object has no attribute 'colour'
class Car: def __init__ (self, model, colour, n_doors): self.model = model self.colour = colour self.n_doors = n_doors car1 = Car("XYZ Ultimate", "Blue", 4) car2 = Car("ABV Extreme", "Yellow", 2) # deleting the entire object del car2 print(car2.model) # Output: NameError: name 'car2' is not defined
“Pass” statement
In Python, classes perform without declaring any coding statements. Therefore, empty classes produce errors. The “pass” statement allows the programs to tackle such issues and avoid the error.
The following is an example of a classes without any statements.
class Car: # Output: IndentationError
The following is an example with the pass statement.
class Car: pass # Output:
__str()__ built-in function
The “__str()__” is a special Python built-in function. It returns a representation of an object in easy-to-understand readable string format.
Without the method, calling class objects direct will produce the id of the object as an output (see below).
class Car: def __init__ (self, model, colour, n_doors): self.model = model self.colour = colour self.n_doors = n_doors car1 = Car("XYZ Ultimate", "Blue", 4) car2 = Car("ABV Extreme", "Yellow", 2) print(car2) # Output: <__main__.Car object at 0x00000227F836D810>
Essentially, the function of “__str()__” regulates what Python should display.
Below is an example with the implementation of the method.
class Car: def __init__ (self, model, colour, n_doors): self.model = model self.colour = colour self.n_doors = n_doors def __str__(self): return "This is " + self.model + " and its featuers." car1 = Car("XYZ Ultimate", "Blue", 4) car2 = Car("ABV Extreme", "Yellow", 2) print(car1) print(car2) # Output: This is XYZ Ultimate and its features. # Output: This is ABV Extreme and its features.