1
+ class MyClass (object ):
2
+ def __init__ (self ):
3
+ self ._some_property = "properties are nice"
4
+ self ._some_other_property = "VERY nice"
5
+ def normal_method (* args ,** kwargs ):
6
+ print ("calling normal_method({0},{1})" .format (args ,kwargs ))
7
+ @classmethod
8
+ def class_method (* args ,** kwargs ):
9
+ print ("calling class_method({0},{1})" .format (args ,kwargs ))
10
+ @staticmethod
11
+ def static_method (* args ,** kwargs ):
12
+ print ("calling static_method({0},{1})" .format (args ,kwargs ))
13
+ @property
14
+ def some_property (self ,* args ,** kwargs ):
15
+ print ("calling some_property getter({0},{1},{2})" .format (self ,args ,kwargs ))
16
+ return self ._some_property
17
+ @some_property .setter
18
+ def some_property (self ,* args ,** kwargs ):
19
+ print ("calling some_property setter({0},{1},{2})" .format (self ,args ,kwargs ))
20
+ self ._some_property = args [0 ]
21
+ @property
22
+ def some_other_property (self ,* args ,** kwargs ):
23
+ print ("calling some_other_property getter({0},{1},{2})" .format (self ,args ,kwargs ))
24
+ return self ._some_other_property
25
+
26
+ o = MyClass ()
27
+ # undecorated methods work like normal, they get the current instance (self) as the first argument
28
+
29
+ o .normal_method
30
+ # <bound method MyClass.normal_method of <__main__.MyClass instance at 0x7fdd2537ea28>>
31
+
32
+ o .normal_method ()
33
+ # normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>,),{})
34
+
35
+ o .normal_method (1 ,2 ,x = 3 ,y = 4 )
36
+ # normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>, 1, 2),{'y': 4, 'x': 3})
37
+
38
+ # class methods always get the class as the first argument
39
+
40
+ o .class_method
41
+ # <bound method classobj.class_method of <class __main__.MyClass at 0x7fdd2536a390>>
42
+
43
+ o .class_method ()
44
+ # class_method((<class __main__.MyClass at 0x7fdd2536a390>,),{})
45
+
46
+ o .class_method (1 ,2 ,x = 3 ,y = 4 )
47
+ # class_method((<class __main__.MyClass at 0x7fdd2536a390>, 1, 2),{'y': 4, 'x': 3})
48
+
49
+ # static methods have no arguments except the ones you pass in when you call them
50
+
51
+ o .static_method
52
+ # <function static_method at 0x7fdd25375848>
53
+
54
+ o .static_method ()
55
+ # static_method((),{})
56
+
57
+ o .static_method (1 ,2 ,x = 3 ,y = 4 )
58
+ # static_method((1, 2),{'y': 4, 'x': 3})
59
+
60
+ # properties are a way of implementing getters and setters. It's an error to explicitly call them
61
+ # "read only" attributes can be specified by creating a getter without a setter (as in some_other_property)
62
+
63
+ o .some_property
64
+ # calling some_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
65
+ # 'properties are nice'
66
+
67
+ o .some_property ()
68
+ # calling some_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
69
+ # Traceback (most recent call last):
70
+ # File "<stdin>", line 1, in <module>
71
+ # TypeError: 'str' object is not callable
72
+
73
+ o .some_other_property
74
+ # calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
75
+ # 'VERY nice'
76
+
77
+ # o.some_other_property()
78
+ # calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
79
+ # Traceback (most recent call last):
80
+ # File "<stdin>", line 1, in <module>
81
+ # TypeError: 'str' object is not callable
82
+
83
+ o .some_property = "groovy"
84
+ # calling some_property setter(<__main__.MyClass object at 0x7fb2b7077890>,('groovy',),{})
85
+
86
+ o .some_property
87
+ # calling some_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{})
88
+ # 'groovy'
89
+
90
+ o .some_other_property = "very groovy"
91
+ # Traceback (most recent call last):
92
+ # File "<stdin>", line 1, in <module>
93
+ # AttributeError: can't set attribute
94
+
95
+ o .some_other_property
96
+ # calling some_other_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{})
97
+ # 'VERY nice'
0 commit comments