xxxxxxxxxx
class A:
#the __init__ function is called when we create an instance of the class by
#writing A() - and the arguments the __init__ function take excluding self
#are the arguments we must provide A(a,b,c) with.
def __init__(self, a, b, c): #constructor that takes 3 arguments (the self is the class itself)
self.first = a #setting the instance attributes to the arguments
self.second = b
self.third = c
variable = A(1,"hi",False) #we must provide a, b, and c
print(variable.first) #1
print(variable.second) #"hi"
print(variable.third) #False
xxxxxxxxxx
class Cat:
def __init__(self, name, age):
# this is the constructor
self.name = name
self.age = age
def __delete__(self):
# this is the destructor
print(f"{self.name} is dead")
def __str__(self):
# this is the string representation
return f"{self.name} is {self.age} years old"
def meow(self):
print(f"{self.name} says meow")
def jump(self):
print(f"{self.name} jumped")
def birthday(self):
self.age += 1
Cat("Tom", 3).meow()
xxxxxxxxxx
# Here’s an example of a function call, a method call, and a call to a function
# inside a module:
import whammy
fizzy()
egg = Wombat()
egg.bluhbluh()
whammy.spam()