Custom Search

Sunday, December 12, 2010

Python Special Class Methods use

Python Special Class Methods use
--------------------------------

In addition to normal class methods, there are a number of special methods that
Python classes can define. Instead of being called directly by your code (like normal methods),
special methods are called for you by Python in particular circumstances or when specific
syntax is used.

http://diveintopython.org/object_oriented_framework/special_class_methods.html
http://diveintopython.org/object_oriented_framework/special_class_methods2.html
http://docs.python.org/reference/datamodel.html#special-method-names

http://stackoverflow.com/questions/2497790/python-overriding-class-not-instance-special-methods

Examples : __new_, __init__, __getattr__, __setattr__, __getattribute__, etc---

----------------------------------------------------------

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* defining __getitem__ on a class make it iterable.
* An object can be iterated over with "for" if it implements __iter__() or __getitem__().


class b:
def __getitem__(self, k):
return k

cb = b()

for k in cb:
print k

OUTPUT
======
1
2
3
.
.
.

========================== 1

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* Overriding special class method __len__ and __getitem__.
* Here trying to iterate over object using 'for'.
* Here trying to find length of object using 'len()'.

class Cust_list_cls:
def __init__(self, seq):
print "----in------__init__-----"
self.lst = seq
def __len__(self):
print "\n---in---__len__-------"
return len(self.lst)
def __getitem__(self, i):
print "\n------i-----", i
try:
return self.lst[-(i + 1)]
except Exception, e:
print "exception : ",e
#Iteration will stop when exception occur.
raise e

lt = [2,4,6]

for x in Cust_list_cls(lt):
print x

print "\n------list length-----",len(Cust_list_cls(lt))


OUTPUT
======

----in------__init__-----

------i----- 0
6

------i----- 1
4

------i----- 2
2

------i----- 3
exception : list index out of range

------list length----- ----in------__init__-----

---in---__len__-------
3

============================== 2

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* Overriding special class method __len__.
* Not overriding special class method __getitem__.
* Here trying to iterate over object using 'for'.
* Here trying to find length of object using 'len()'.

class Cust_list_cls:
def __init__(self, seq):
print "\n----in------__init__-----"
self.lst = seq
def __len__(self):
print "\n---in---__len__-------"
return len(self.lst)


lt = [2,4,6]

print "\n------list length-----",len(Cust_list_cls(lt))

for x in Cust_list_cls(lt):
print x

OUTPUT
======

------list length-----
----in------__init__-----

---in---__len__-------
3

----in------__init__-----
Traceback (most recent call last):
File "aa.py", line 41, in
for x in Cust_list_cls(lt):
TypeError: iteration over non-sequence

=================================== 3

* Define a class which acts as a sequence (list) and give the elements in reverse order.
* Overriding special class method __getitem__.
* Not overriding special class method __len__.
* Here trying to iterate over object using 'for'.
* Here trying to find length of object using 'len()'.

class Cust_list_cls:
def __init__(self, seq):
print "\n----in------__init__-----"
self.lst = seq

def __getitem__(self, i):
print "\n------i-----", i
#Iteration will stop when exception occur.
return self.lst[-(i + 1)]

lt = [2,4,6]

for x in Cust_list_cls(lt):
print x

print "\n------list length-----",len(Cust_list_cls(lt))


OUTPUT
=======

----in------__init__-----

------i----- 0
6

------i----- 1
4

------i----- 2
2

------i----- 3

------list length-----
----in------__init__-----
Traceback (most recent call last):
File "aa.py", line 42, in
print "\n------list length-----",len(Cust_list_cls(lt))
AttributeError: Cust_list_cls instance has no attribute '__len__'

=====================================

No comments:

Post a Comment