py_tip_08.py

#!/usr/bin/python3
# ==================================================================
# From YouTube video:
#    10 Python Tips and Tricks For Writing Better Code
#===================================================================

class Person():
    pass

print('------------------------------')
# object with no atttributes
person = Person()

# add attributes to object
person.first = 'George'
person.last  = 'Jungle'

# print object's attributes
print(person.first)
print(person.last)

print('------------------------------')
# set object's attribute to a variable's value
# using the setattr function

personx = Person()

first_key = 'first'
first_val = 'tomtom'

setattr(personx, first_key, first_val)

print(personx.first)

# get the valuse of an object's attribute using a varaible

first = getattr(personx, first_key)

print(first)