File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11/.idea
2+ * .pyc
Original file line number Diff line number Diff line change 55
66import time
77import sys
8+ import functools
89# function decorator
910
1011
@@ -63,7 +64,6 @@ class Foo(object):
6364 pass
6465
6566
66-
6767class Human (object ):
6868 def __init__ (self , func ):
6969 self .name = None
@@ -84,6 +84,30 @@ def eat(self):
8484def someone ():
8585 pass
8686
87+
88+ def CatDecorator (cls ):
89+
90+ @functools .wraps (cls )
91+ class new_class :
92+ def __init__ (self , name ):
93+ cls .__init__ (self , name )
94+ self .sex = "male"
95+
96+ def set_sex (self , sex ):
97+ self .sex = sex
98+
99+ def get_set (self ):
100+ return self .sex
101+
102+ return new_class
103+
104+
105+ @CatDecorator
106+ class Cat (object ):
107+ def __init__ (self , name ):
108+ self .name = name
109+
110+
87111if __name__ == "__main__" :
88112 # function decorator
89113 print "\n ----------function decorator----------"
@@ -100,3 +124,7 @@ def someone():
100124 Tim .breath ()
101125 Tim .eat ()
102126 Bob = someone ()
127+
128+ Jack = Cat ("Jack" )
129+ Jack .set_sex ("femal" )
130+ print Jack .get_set ()
You can’t perform that action at this time.
0 commit comments