my net house

WAHEGURU….!

Python For Yankees

Before going further reading about Class Development I would like you to tell
one important thing:

Most of the uses of inheritance can be simplifi ed or replaced with composition, and multiple
inheritance should be avoided at all costs.

I am sure after this you will be able to read lots and lots of code written in
python. 🙂 If you want to do it fast, Do it well. 🙂

Python Class Development Toolkit.

def __init__():
pass

Don’t put anything in instance that you don’t need instance for.

def i_am_class_method(self):
return ‘takes self as argument’

If you need to shared data for whole class then put it on class level,
not on instance level.

class IAm(object):

shared_data=[1,2,3,4,5]

def __init__(self):
print shared_data

def fun_2(self):
print shared_data

Iron_clad_rule:- in java or C++ is–>do not expose your attributes

Subclassing is just like inhertance. Data+methods:

Class NewOne(IAm):

def fun3(self):
return IAm.fun_2(self)

Multiple/Alternative-Constructors:(When you need to change the behavior
of class’s data by just calling an intensive function):

class Circle(object):

def __init__(self,radius):
self.radius=radius

def fun_2(self):
print self.radius

@classmethod   # Alternative constructive
def from_bbd(cls,new_radius):
“””construct a circle from bounding box diagnol”””

raduis = new_radius/2.0/math.sqrt(2.0)
return Circle(radius)

Make Alternate constructor to work for Subclass as well:

class Circle(object):

def __init__(self,radius):
self.radius=radius

def fun_2(self):
print self.radius

@classmethod   # Alternative constructive
def from_bbd(cls,new_radius):
“””construct a circle from bounding box diagnol”””

raduis = new_radius/2.0/math.sqrt(2.0)
return cls(radius)

Independent methods inside class: – Why we ever need those?
Think of situation where everything in your code breaks but you just need to tell
user something and something and all you need is 🙂 Static-Method.!!

class Circle(object):

def __init__(self,radius):
self.radius=radius

def fun_2(self):
print self.radius

@staticmthod
def just_method(just_parameter):
return ‘I am independent’

Getters and Setters in Python:(Access Data,change data on the fly)
As in the other languages there are inbuilt methods to access data from
class as well as change values of method attributes in the class.

Python uses the property decorator to perform such operations:

class Person(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name

@property
def full_name(self):
return self.first_name + ‘ ‘ + self.last_name

@full_name.setter
def full_name(self, value):
first_name, last_name = value.split(‘ ‘)
self.first_name = first_name
self.last_name = last_name

@full_name.deleter
def full_name(self):
del self.first_name
del self.last_name

Slots in Pyhton:
When you have lots and lots of things to perform or do in the way that
your class instance is consuming HUGE memory then you must use SLOTS.

Earlier approach:

class MyClass(object):
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
self.set_up()

Approach after SLOTS:

class MyClass(object):
__slots__ = ['name', 'identifier']
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
self.set_up()

Advertisements

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: