-
Notifications
You must be signed in to change notification settings - Fork 49
/
attribute_obj.py
27 lines (21 loc) · 1.01 KB
/
attribute_obj.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
class Person(object):
name = 'twtrubiks'
if __name__ == "__main__":
p = Person()
print('Before Set: {}'.format(p.name))
# The setattr() method sets the value of given attribute of an object.
# setattr(object, name, value)
setattr(p, 'name', 'rubiks')
print('After Set: {}'.format(p.name))
# The getattr() method returns the value of the named attribute of an object
# getattr(object, name[, default])
# default (Optional) - value that is returned when the named attribute is not found
print('The age is: {}'.format(getattr(p, 'age', 'default_20')))
# print('The age is:'.format(p.age)) # error
# creates a new attribute
setattr(p, 'age', 18)
print('After Set afe: {}'.format(getattr(p, 'age', 'default_20')))
# The hasattr() method returns true if an object has the given named attribute and false if it does not.
# hasattr(object, name)
print('Person has name?: {}'.format(hasattr(p, 'name')))
print('Person has salary?: {}'.format(hasattr(p, 'salary')))