Py13: Class and Object
Class is a logical grouping of data and function defined by user. Class definition is used by python to create an objects. Each object has its own attributes and behaviors.
Defining class
To define class, begin with class
keyword, and followed by the class name and a colon. By convention, class names begins with capital letter and no space so use the camel case.
class ClassName: pass
Defining Variable
class ClassName: attributeName = value
Defining method
class ClassName: def methodName(self): pass
the self parameter represents the instance of class. This parameter allows access to variable, attributes and methods defined in the class.
For example, following class named Person has two attributes (name and age) and a method called displayPerson. Notice we use the self parameter, which makes the methods belongs to instance of class.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def displayPerson(self):
print("Name " , self.name , " Age " , self.age)
The self parameter is alwways the first parameter in the method's parameter list.
Creating object
To create an object, write class name and followed by parentheses.
objectRef = ClassName()
Example, creating an object called citizen1
of Person data type.
citizen1 = Person("Civa", 30)
Accessing attribute and method
Attribute are accessed using dot operator
objectRef.attributeName
method
objectRef.methodName()
citizen1 = Person("Civa", 30)
citizen1.displayPerson()
citizen1.age = 40
citizen1.displayPerson()
Output
Name Civa Age 30 Name Civa Age 40
Constructor
Earlier we say oddly named method that begins with double underscore. It is called constructor. Constructor is defined by __init__
as it's name. The main purpose of constructor is to create and initialize the object attributes.
def __init__(self, parameters): initialization statements
Type of attributes
There are two types of attributes and they are Instance attribute and class attribute. A Class attribute is declared after the class declaration and outside the __init__ function. Meanwhile, an instance attributes are declared inside __init__ function. Class attribute can be accessed by all instance created by the class and Instance attributes belongs to the particular instance of the class.
class ClassName: classAttribute = value def __init__(self, parameters): self.instanceAttribute = value
class Person:
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Person.count = Person.count + 1
def displayPerson(self):
print("Name " , self.name , " Age " , self.age)
citizen1 = Person("Civa", 30)
citizen1.displayPerson()
citizen2 = Person("Ann", 28)
citizen2.displayPerson()
print("No. of citizens : " , Person.count)
Output
Name Civa Age 30 Name Ann Age 28 No. of citizens : 2
Types of method
There are three type of methods in python. Class method, static method and instance method. We already familiar with the instance method that uses self
parameter.
Class method
Class method belongs to class not to the instance. It has cls parameter that refers to class. It can modify class attributes that can be reflected by all instance that belongs to the class.
class className: def methodName(cls, parameters): statements
A class method can be called using the class name without creating an instance
ClassName.classMethodName(parameters)
static method
It does not specify particular first argument (self or cls). It bounds to class and not to instance. It cannot modify class attributes.
class className: def methodName(cls, parameters): statements
A static method can be called using the class name without creating an instance
ClassName.staticMethodName(parameters)
Comments
Post a Comment