Skip to content

Commit 32241cb

Browse files
committed
decorator on class
1 parent 287a92a commit 32241cb

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/.idea
2+
*.pyc

decorator.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import time
77
import sys
8+
import functools
89
# function decorator
910

1011

@@ -63,7 +64,6 @@ class Foo(object):
6364
pass
6465

6566

66-
6767
class Human(object):
6868
def __init__(self, func):
6969
self.name = None
@@ -84,6 +84,30 @@ def eat(self):
8484
def 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+
87111
if __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()

0 commit comments

Comments
 (0)