A class is a structure in Python that can be used as a blueprint to create objects that have
We want to define a class called Client in which a new instance stores a client's name, balance, and account level. It will take the format of:
class Client(object): def __init__(self, args[, ...]) #more code
__init__" is what we use when creating classes to define how we can create a new instance of this class.
The arguments of
__init__ are required input when creating a new instance of this class, except for 'self'.
In :# create the Client class below class Client(object): def __init__(self, name, balance): self.name = name self.balance = balance + 100 #define account level if self.balance < 5000: self.level = "Basic" elif self.balance < 15000: self.level = "Intermediate" else: self.level = "Advanced"
The attributes in Client are name, balance and level.
Note: "self.name" and "name" are different variables. Here they represent the same values, but in other cases, this may lead to problems. For example, here the bank has decided to update "self.balance" by giving all new members a bonus $100 on top of what they're putting in the bank. Calling "balance" for other calculations will not have the correct value.
In :John_Doe = Client("John Doe", 500) Jane_Defoe = Client("Jane Defoe", 150000)
We can see the attributes of John_Doe, or Jane_Defoe by calling them:
In :John_Doe.name Jane_Defoe.level Jane_Defoe.balance
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-6-fcac2ded7a1c> in <module>() 2 Jane_Defoe.level 3 Jane_Defoe.balance ----> 4 Client(Jane_Defoe, name) NameError: name 'name' is not defined
We can also add, remove or modify attributes as we like:
In :John_Doe.email = "firstname.lastname@example.org" John_Doe.email = "email@example.com" del John_Doe.email
In :getattr(John_Doe, 'name') setattr(John_Doe, 'email', 'firstname.lastname@example.org') John_Doe.email
You can also use the following instead instead of the normal statements:
getattr(obj, name[, default]) : to access the attribute of object.
hasattr(obj,name) : to check if an attribute exists or not.
setattr(obj,name,value) : to set an attribute. If attribute does not exist, then it would be created.
delattr(obj, name) : to delete an attribute.
A class attribute is an attribute set at the class-level rather than the instance-level, such that the value of this attribute will be the same across all instances.
For our Client class, we might want to set the name of the bank, and the location, which would not change from instance to instance.
In :Client.bank = "TD" Client.location = "Toronto, ON"
In :# try calling these attributes at the class and instance level Client.bank Jane_Defoe.bank
Methods are functions that can be applied (only) to instances of your class.
For example, in the case of our 'Client' class, we may want to update a person's bank account once they withdraw or deposit money. Let's create these methods below.
Note that each method takes 'self' as an argument along with the arguments required when calling this method.
In :# Use the Client class code above to now add methods for withdrawal and depositing of money # create the Client class below class Client(object): def __init__(self, name, balance): self.name = name self.balance = balance + 100 #define account level if self.balance < 5000: self.level = "Basic" elif self.balance < 15000: self.level = "Intermediate" else: self.level = "Advanced" def deposit(self, amount): self.balance += amount return self.balance def withdraw(self, amount): if amount > self.balance: raise RuntimeError("Insufficient for withdrawal") else: self.balance -= amount return self.balance
*not in the philosophical sense
In the method, withdraw(self, amount), the self refers to the instance upon which we are applying the instructions of the method.
When we call a method,
f(self, arg), on the object
x, we use
xis passed as the first argument, self, by default and all that is required are the other arguments that comprise the function.
It is equivalent to calling
Try it yourself with the Client class and one of the methods we've written.
In :# Try calling a method two different ways John_Doe.deposit(500) Client.withdraw(Jane_Defoe, 50000)
Static methods are methods that belong to a class but do not have access to self and hence don't require an instance to function (i.e. it will work on the class level as well as the instance level).
We denote these with the line
@staticmethod before we define our static method.
Let's create a static method called make_money_sound() that will simply print "Cha-ching!" when called.
In :# Add a static method called make_money_sound() # create the Client class below class Client(object): def __init__(self, name, balance): self.name = name self.balance = balance + 100 #define account level if self.balance < 5000: self.level = "Basic" elif self.balance < 15000: self.level = "Intermediate" else: self.level = "Advanced" @staticmethod def make_money_sound(): print "Cha-ching!"
A class method is a type of method that will receive the class rather than the instance as the first parameter. It is also identified similarly to a static method, with
Create a class method called bank_location() that will print both the bank name and location when called upon the class.
In :# Add a class method called bank_location() # create the Client class below class Client(object): bank = "TD" location = "Toronto, ON" def __init__(self, name, balance): self.name = name self.balance = balance + 100 #define account level if self.balance < 5000: self.level = "Basic" elif self.balance < 15000: self.level = "Intermediate" else: self.level = "Advanced" @classmethod def bank_location(cls): return str(cls.bank + " " + cls.location)
Out:'TD Toronto, ON'
A 'child' class can be created from a 'parent' class, whereby the child will bring over attributes and methods that its parent has, but where new features can be created as well.
This would be useful if you want to create multiple classes that would have some features that are kept the same between them. You would simply create a parent class of these children classes that have those maintained features.
Imagine we want to create different types of clients but still have all the base attributes and methods found in client currently.
For example, let's create a class called Savings that inherits from the Client class. In doing so, we do not need to write another
__init__ method as it will inherit this from its parent.
In :# create the Savings class below class Savings(Client): interest_rate = 0.005 def update_balance(self): self.balance += self.balance*self.interest_rate return self.balance
In :# create an instance the same way as a Client but this time by calling Savings instead Lina_Tran = Savings("Lina Tran", 50)
In :# it now has access to the new attributes and methods in Savings... print Lina_Tran.name print Lina_Tran.balance print Lina_Tran.interest_rate
Lina Tran 150 0.005
In :# ...as well as access to attributes and methods from the Client class as well Lina_Tran.update_balance()
In :#defining a method outside the class definition def check_balance(self): return self.balance Client.check_balance = check_balance