Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81681c3

Browse files
Add demo for decorator
1 parent 8b1b1fe commit 81681c3

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

‎Decorators/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Decorators</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

‎Decorators/.pydevproject

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
4+
<path>/${PROJECT_DIR_NAME}/src</path>
5+
</pydev_pathproperty>
6+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.6</pydev_property>
7+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
8+
</pydev_project>

‎Decorators/src/decoratorsdemo.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
Created on Aug 6, 2017
3+
4+
@author: Aditya
5+
6+
'''
7+
8+
class GOT:
9+
def __init__(self, **kargs):
10+
self.properties = kargs
11+
12+
def what(self):
13+
print('GOT is Game of Thrones!!')
14+
15+
def purpose(self):
16+
print('GOT tells the story of 7 kingdoms and the ruler of those kingdoms.')
17+
18+
def get_properties(self):
19+
return self.properties
20+
21+
def get_property(self, key):
22+
return self.properties.get(key, None)
23+
24+
@property
25+
def kingdom_animal(self):
26+
return self.properties.get('kingdom_animal', None)
27+
28+
@kingdom_animal.setter
29+
def kingdom_animal(self, a):
30+
self.properties['kingdom_animal'] = a
31+
32+
@kingdom_animal.deleter
33+
def kingdom_animal(self):
34+
del self.properties['kingdom_animal']
35+
36+
def main():
37+
print('''Decorators in python are used to create accessor methods for variables.''')
38+
houseStark = GOT(kingdom_animal = 'Wolf', location = 'North')
39+
print(houseStark.get_properties())
40+
print(houseStark.get_property('kingdom_animal'))
41+
print(houseStark.get_property('location'))
42+
43+
print('\nDemo of Decorators:')
44+
print('Getter:', houseStark.kingdom_animal)
45+
46+
del houseStark.kingdom_animal
47+
48+
print(houseStark.kingdom_animal)
49+
50+
houseStark.kingdom_animal = 'Lion'
51+
print(houseStark.kingdom_animal)
52+
53+
54+
if __name__ == '__main__': main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /