Week 6 subject 1: Data Members of Classes
- Data members of Classes are created when they are assigned values.
- Data members can be dynamically added or deleted (with del statement).
- Parent and child Classes can have data members with the same name, when referred to, child will override parent.
- ADVANCED: Data members can be attached to the Class as well as the instance. In the former they are called Class Variables, latter Instance Variables. Class Variable values are common across all instances of the Class, while Instance Variable values are local to the exact instance.
class Car(vehicle):NumOfWheels = 4
def move(self, direction, distance):
pass
myCar.NumOfWheels = 5
print(Car.NumOfWheels, myCar.NumOfWheels)
myCar = Car()
Week 6 subject 2: Methods
- Methods are functions attached to Classes. Like functions they need to be defined before being used.
- Methods have 1 implicit parameter: self. self means the instance itself. methods use the self variable to access other members of the instance.
class Car(vehicle):
NumOfWheels = 4
def move(self, direction, distance):
pass
def showwheels(self):
print(self.NumOfWheels)
- Like Data Members, parent and child classes can define methods with the same names. When called, child will override parent.
- ADVANCED: Methods can also be added to Classes after initial definition of Classes. Methods can be attached to both the Class and the Instance. This is too complex, don't try.
No comments:
Post a Comment