simple class defination - its just a logical grouping of objects and methods
suppose we want a define a person object and with some attributes and methods on the person objects. we do it like this.
def __init__(self, age, gender, location, contact): This is called the constructor of the class.
as name suggests it is used to
class instance from the
class defination of
A class can be considered as a blueprint or instruction manula of how the class should be constructed. until now memory is not allocated for the class above its just a blueprint/instruction manual.
What if we want to use above class? for that we need to create a
class instance or an
object of class
This is how we do it:
The above is like asking python “ if you happen to know how to create a
User ,then create it with the values that i provide and remember to follow the instruction manual for creating a User
and then to use its attributes
John is just one instance of User class yo ucan create any number of instances of a class you want.
Now whats going on with that
self in function definations inside class?
refers to instance itself that we created. calling
John..setLocation('UK') puts those instruction to John instance. simply put
self is the instance of the John that
setLocation is being called upon.you could have written
# Instance Level v/s Class Lvel.
until now all we saw was creating attributes and methods at instance level i.e each class has its own copy of its attributes.What if we wanted some attribute to be present at class level?
its actually quite simple. This is how its done
now every object of
User will have a common value