root/supersingleton/single.py

Revision 1203, 1.2 kB (checked in by efphe, 1 year ago)

moving code from fenilot.org

Line 
1 class A(object):
2
3     exporting_methods= ['seta','geta']
4     @staticmethod
5     def _ready():
6         return getattr(A, '_the_instance', None)
7    
8     def __new__(cls, *p, **k):
9
10         i= A._ready()
11         if i is None:
12             A._the_instance= object.__new__(A)
13             if cls is A:
14                 return A._the_instance
15             A.__init__(A._the_instance)
16             i= A._the_instance
17
18         o= object.__new__(cls)
19
20         for attr_name in A.exporting_methods:
21             setattr(o, attr_name, getattr(i, attr_name))
22
23         return o
24
25     def __init__(self, a=1):
26         self.a=a
27
28     def geta(self):
29         print self.a
30
31     def seta(self, v):
32         self.a= v
33
34     def prima(self):
35         print 'prima'
36         self.prima= self.dopo
37
38     def dopo(self):
39         print 'dopo'
40
41 class B(A):
42     def __init__(self, x):
43         self.x= x
44
45 class C(A):
46     def __init__(self, z):
47         self.z= z
48
49 # se commenti la seguente, AA.a sara' 1:
50 # i valori di __init__ di A non possono essere espressi.
51 #a=A(1999)
52
53 #b= B(77)
54 #n= B(66)
55 #b.geta()
56 #n.seta(666)
57 #b.geta()
58
59
60 class AA:
61     def __init__(self):
62         pass
63     def az(self):
64         self.pippo=1
65
66 class BB:
67     def __init__(self):
68         self.medium= AA()
Note: See TracBrowser for help on using the browser.