Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def greeting(person):
- print("My name {}, and I am a {}".format(
- getattr(person, "get_name")(),
- getattr(person, "get_employment")(),
- ))
- def get_name(person):
- return getattr(person, "name")
- def get_employment(person):
- return getattr(person, "employment")
- def getattr(person, attr):
- if attr in person:
- return person[attr]
- if attr not in person["base"]:
- raise AttributeError(f"attr {attr} not found")
- val = person["base"][attr]
- if callable(val):
- def func(*args, **kwargs):
- return val(person, *args, **kwargs)
- return func
- return val
- def create(base, *args, **kwargs):
- new_item = base["new"](base)
- base["init"](new_item, *args, **kwargs)
- return new_item
- def new(base):
- return {"base": base}
- def init(person, name, employment=None):
- person["name"] = name
- if employment is not None:
- person["employment"] = employment
- person_base = {
- "employment": "Unemployed",
- "greeting": greeting,
- "get_name": get_name,
- "get_employment": get_employment,
- "new": new,
- "init": init
- }
- # --------------------
- person_1 = create(person_base, "John", "Doctor")
- person_2 = create(person_base, "Mathew", "Teacher")
- person_3 = create(person_base, "Mathew")
- # --------------------
- getattr(person_1, "greeting")()
- getattr(person_2, "greeting")()
- getattr(person_3, "greeting")()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement