Created for
class Person():
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
# should be __count, but good to show that Person.count != self.count
self.count = Person.count
# increment objects counter:
Person.count += 1
def __str__(self):
return "{}. {}: {}".format(self.count, self.name, self.age)
maria = Person("Maria", 20)
pesho = Person("Pesho", 30)
print(maria)
print(pesho)
@classmethod
decorator
class Spam():
@classmethod
def foo(cls, arg1, arg2, ...):
cls
will be bound to class, not to instance.
class Person():
count = 0
@classmethod
def increment_counter(cls):
cls.count += 1
print("count:",cls.count )
def __init__(self, name, age):
self.name = name
self.age = age
self.increment_counter()
# attach count to an object
self.count = Person.count
def __str__(self):
return "{}. {}: {}".format(self.count, self.name, self.age)
maria = Person("Maria", 20)
pesho = Person("Pesho", 30)
print(maria)
print(pesho)
# obviously, we would not want that. So, be careful with class methods!
maria.increment_counter()
pesho.increment_counter()
class Spam():
@staticmethod
def foo(arg1, arg2, ...):
Note, that no implicit argument is passed to foo()
class Person():
@staticmethod
def validate_age(age):
if not 0 < age < 100:
raise ValueError("Invalid age value")
def __init__(self, name, age):
self.name = name
try:
self.validate_age(age)
except:
raise
else:
self.age = age
def __str__(self):
return "{}: {}".format(self.name, self.age)
maria = Person("Maria", 20)
print(maria)
pesho = Person("Pesho", 300)
print(pesho)
class A():
@staticmethod
def staticMethod():
print("STATIC method fired!")
print("Nothing is bound to me (but I'm defined inside a class)")
print("~" * 30)
@classmethod
def classMethod(cls):
print("CLASS method fired!")
print(str(cls) + " is bound to me")
print("~" * 30)
# normal method
def normalMethod(self):
print("'normalMethod' fired!")
print(str(self) + " is bound to me")
print("~" * 30)
a = A()
a.staticMethod()
a.classMethod()
a.normalMethod()
class DerivedClassName(BaseClassName):
pass
class Person():
"""docstring for Person"""
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "{} is {} years old".format(self.name, self.age)
class Employee(Person):
pass
pesho = Employee("Pesho",25)
print(pesho)
class Person():
"""docstring for Person"""
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "{} is {} years old".format(self.name, self.age)
class Employee(Person):
def __str__(self):
return "{} is employee".format(self.name)
maria = Person("Maria", 20)
pesho = Employee("Pesho",25)
print(maria)
print(pesho)
We can call the base class method directly
BaseClass.method(self, arguments)
class Person():
"""docstring for Person"""
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "{} is {} years old".format(self.name, self.age)
class Employee(Person):
"""docstring for Employee"""
def __init__(self, name, age, salary):
Person.__init__(self,name,age)
self.salary = salary
pesho = Employee("Pesho",25, 3500)
print(pesho)
super()
access to base methodsWhen we need to access inherited methods that have been overridden in a class, it's preffered to use super()
class C(B):
def method(self, arg):
super().method(arg)
super()
access to base methods - example
class Person():
"""docstring for Person"""
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "{} is {} years old".format(self.name, self.age)
class Employee(Person):
"""docstring for Employee"""
def __init__(self, name, age, salary):
super().__init__(name,age)
self.salary = salary
def __str__(self):
# return super().__str__() + ". Has salary: {}".format(self.salary)
return Person.__str__(self) + ". Has salary: {}".format(self.salary)
pesho = Employee("Pesho",25, 3500)
print(pesho)
class Person():
"""docstring for Person"""
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "{} is {} years old".format(self.name, self.age)
class Employee(Person):
"""docstring for Employee"""
def __init__(self, name, age, salary):
super().__init__(name,age)
self.salary = salary
def __str__(self):
# return super().__str__() + ". Has salary: {}".format(self.salary)
return Person.__str__(self) + ". Has salary: {}".format(self.salary)
def __add__(self,other):
return self.salary + maria.salary
pesho = Employee("Pesho",25, 3500)
maria = Employee("maria",20, 2500)
print(pesho)
print(maria)
print("pesho + maria = ",pesho + maria)
# pesho + maria = 6000
If it walks like a duck and it quacks like a duck, then it must be a duck
class Duck:
def quack(self):
print('Quack, Quack')
class Goose:
def quack(self):
print('Quack!')
def quack(obj):
obj.quack()
donald = Duck()
lea = Goose()
quack(donald)
quack(lea)
class Car:
def __init__(self, engine):
self.engine = engine
def run(self):
self.engine.start()
class Engine:
def start(self):
print("Engine started!")
trinity5_8 = Engine()
ford = Car(trinity5_8)
ford.run()
These slides are based on
customised version of
framework