It’s
time to learn about core functionality of Python programming language and that
is Python objects and classes. In this lesson you’ll learn how to create it and
use it in your Python program. As mentioned many times before Python is an
object oriented programming language just like C++ and many others. Just to
clarify the difference between procedural and object oriented programming the
emphasis in procedural programming is on functions while in object oriented
programming the main focus are objects.
Collection
of data (variables) and methods (functions) that act on those data are what
make objects. We can think of a class as a sketch or prototype. Class is like a
sketch of a car because it contains all the details about tires, engine,
lights, seats … Based on this description a car is built and the car is an
object.
So
if the class is a blueprint for building a car we can conclude that we can
create many objects from this class. You will find in literature that object is
an instance of a class and the process of creating this object is called
instantiation.
How to Create a Class?
Now
let’s start with something simple. Let’s create a simple class in Python. To
define a class use the keyword class. After the class, give a name of the class
and the first line after class declaration is usually used for a brief
description about the class. This line (string) is called docstring and while
it’s not mandatory it is usually recommended (just to know why you created this
class).
class myFirstclass:
"This
line is a docstring and it usualy say something about a class"
Pass
A
class creates a new local namespace where all its attributes are defined.
Attributes may be data or functions. There are special attributes that can be
used and they begin with double underscore ‘__’. An example of that attribute
is __doc__ and it gives us the docstring of that class.
>>>
class myFirstclass:
... "This line is a docstring and it
usualy say something about a class"
... pass
...
>>>
myFirstclass.__doc__
|
'This
line is a docstring and it usualy say something about a class'
|
When
we create a class, a new class object is created with the same name. This class
object allows us to access the different attributes as well as to instantiate
new objects of that class.
class mySecondclass:
"This
is my second class"
a = 50
def
function(self):
print('Hello')
print 'a = ' + str (mySecondclass.a)
a = 50
print mySecondclass.function
print "DOCSTRING = " +
str(mySecondclass.__doc__)
DOCSTRING = This is my second class
How to Create an Object?
We
saw that the class object could be used to access different attributes. It can
also be used to create new object instances (instantation) of that class. The
procedure of creating an object is similar to a function call.
obj = mySecondclass()
This
command will create a new instance of an object named obj. We can access
attributes of objects using the object name as prefix.
class
mySecondclass:
"This is my second class"
a = 50
def function(self):
print('Hello')
obj =
mySecondclass()
""
Self
- parameter is parameter that will always be used as reference back to the
object itself. The object itself is passed as first argument. So obj.function()
translates to mySecondclass.func(obj). In general calling a method with a list
of n arguments is equivalent to calling the corresponding function with an
argument list that is created by inserting methods object before the first
argument. For that reason the first argument of the function in the class must
be object itself.
No comments:
Post a Comment