Little Bit about Python Classes

Python classes are different from C structures and Java classes. The main difference is, when we define a structure in C or class in Java there is no memory allocation. But for a Python class at the time of definition itself there is memory allocation. i.e. Python classes are concrete datatypes whereas others are only abstract datatype.

Another difference is, at runtime we can modify the Python classes. But this is not possible in Java and C. Hence, Python classes are flexible.

Creating a class is similar to function definition. For creating a class abc:

>>> class abc:

…       pass

a class abc with a particular location in memory obtained.

>>> abc.p=10

create an attribute p with value 10.

Now, we can create an instance of this class, similar to creation of an object of a class in Java, but easy.

>>> m=abc()

>>> m.p

10

If we try to execute the command ‘m.p’, the interpreter will first check in the object for ‘p’. If ‘p’ is absent, as in this case, interpreter will check ‘m’sclass, i.e. ‘abc’,. Interpreter finds ‘p’ there and prints ‘10′. If ‘p’ is present inside m, the value of ‘p’ will be its value in ‘m’.

>>> m.p=20

>>> m.p

20

If we delete ‘p’ from ‘abc’ using ‘del abc.p’, we get the value of ‘m.p’ as 20, but ‘abc.p’ prints error message.

Methods Inside a Class

>>> class abc:

…        p=10

…        def fun():

…                 print ‘hai’

>>> m=abc()

>>> m.p

10

>>> m.fun()

‘error’

The reason for the ‘error’ is: when we call the function ‘m.fun()’, the interpreter pass an additional parameter with it. The function in the class is defined with no parameters. The additional parameter is the object reference of ‘m’. The convention is that we should use ‘self’ as the parameter name. So the following change in the function ‘fun’ will print the output as ‘hai’.

>>> class abc:

…    def fun(self):

…        print ‘hai’

If we create a function named ‘__init__’ in the class, whenever we create an instance of that class the function ‘__init__’ will be executed.

The following is another interesting example of class:

>>> class complex:

…     def __init__(self,a,b):

…          self.re=a

…          self.im=b

>>> m=complex(1,2)

>>> m.re

1

>>> complex.re

‘error’

The reason is ‘re’ and ‘im’ are local to ‘fun’. Only an instance of class ‘complex’ can access that attribute. Another reason for why its possible for ‘m’ without use of function name is, the function is’__init__’, which will be executed whenever an instance of ‘complex’ is created.

Advertisements

About Odol Shinu

I've completed my B Tech in Information Technology in 2010 from Government Engineering College Sreekrishnapuram Palakkad under Calicut University.

Posted on August 31, 2010, in Python. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: