Override predefined function : Class Virtual « Class « Python

Home
Python
1.2D
2.Application
3.Buildin Function
4.Class
5.Data Structure
6.Data Type
7.Database
8.Development
9.Dictionary
10.Event
11.Exception
12.File
13.Function
14.GUI Pmw
15.GUI Tk
16.Language Basics
17.List
18.Math
19.Network
20.String
21.System
22.Thread
23.Tuple
24.Utility
25.XML
Python » Class » Class VirtualScreenshots 
Override predefined function
Override predefined function



class MyList:
    def __init__(self, start):
        #self.wrapped = start[:]                
        self.wrapped = []                       
        for x in start: self.wrapped.append(x)
    def __add__(self, other):
        return MyList(self.wrapped + other)
    def __mul__(self, time):
        return MyList(self.wrapped * time)
    def __getitem__(self, offset):
        return self.wrapped[offset]
    def __len__(self):
        return len(self.wrapped)
    def __getslice__(self, low, high):
        return MyList(self.wrapped[low:high])
    def append(self, node):
        self.wrapped.append(node)
    def __getattr__(self, name):                
        return getattr(self.wrapped, name)
    def __repr__(self):
        return 'self.wrapped'

class MyListSub(MyList):
    calls = 0                                  

    def __init__(self, start):
        self.adds = 0                          
        MyList.__init__(self, start)

    def __add__(self, other):
        MyListSub.calls = MyListSub.calls + 1  
        self.adds = self.adds + 1              
        return MyList.__add__(self, other)

    def stats(self):
        return self.calls, self.adds           

if __name__ == '__main__':
    x = MyListSub('A')
    y = MyListSub('B')
    print x[0]
    print x[1:]
    print x + ['C']
    print x + ['D']
    print y + ['E']
    print x.stats()
    
    

           
       
Related examples in the same category
1.Virtual function: extend method behaviourVirtual function: extend method behaviour
2.Virtual function in PythonVirtual function in Python
3.Virtual function in Python classVirtual function in Python class
4.Virtual function in two base classVirtual function in two base class
5.Virtual function in derived class is calledVirtual function in derived class is called
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.