-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass_defining.py
44 lines (31 loc) · 897 Bytes
/
class_defining.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python
# Date: 2017-mm-dd
#
# Description:
# Sample class declaration and usage of getattr and setattr function.
class student:
"""A class representing student's attributes"""
def __init__(self, n, a):
self.full_name = n
self.age = a
def get_age(self):
return self.age
b = student('some-name', 27)
print(b.get_age()) # 27
print(b.full_name) # some-name
print(b.age) # 27
b.age = 50
print(b.age) # 50
print(b.get_age()) # 50
print('getattr function...')
print(getattr(b, 'full_name')) # some-name
# Prints functions reference.
# <bound method student.get_age of <__main__.student instance at 0x7fdf3ebb0950>>
print(getattr(b, 'get_age'))
# Calls get_age()
# 50
print(getattr(b, 'get_age')())
print('hasattr function...')
print(hasattr(b, 'age')) # True
print(hasattr(b, 'get_age')) # True
print(hasattr(b, 'get_age123')) # False