OO in python

self,instance variable,instance method,class variable, class variable

""" circle module."""
class Circle:
    """Circle class"""
    all_circles = []
    pi = 3.14159
    def __init__(self,r=1):
        self.radius =  r
        self.__class__.all_circles.append(self)
    def area(self):
        return self.__class__.pi*self.radius*self.radius
    @staticmethod
    def total_area():
        total = 0
        for c in Circle.all_circles:
            total = total +c.area()
        return total

property of python  

class Ab(object):
    def __init__(self,data):
        self.data = data
        self._numrows = len(data)
    @property
    def numrows(self):
        print ("peeked")
        return self._numrows
    @numrows.setter
    def numrows(self,x):
        self._numrows = x
        print ("set")

Yes, property are decorator of python. like property is compiler directive for objective-c

 

 

 

 

 

posted on 2012-08-14 16:10  grep  阅读(218)  评论(0编辑  收藏  举报