Skip to content

Instantly share code, notes, and snippets.

@StephenFordham
Created June 9, 2022 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephenFordham/73425f582ff98b60a2452f48955c6dbd to your computer and use it in GitHub Desktop.
Save StephenFordham/73425f582ff98b60a2452f48955c6dbd to your computer and use it in GitHub Desktop.
Call_Magic_Method
class Employees(object):
def __init__(self, name, age, location):
self._name = name
self._age = age
self._location = location
def __call__(self, *args, **my_attr_dict):
if len(my_attr_dict) > 0:
try:
self._name = my_attr_dict['_name']
self._location = my_attr_dict['_location']
self._age = my_attr_dict['_age']
except KeyError:
pass
else:
try:
self._name = args[0]
self._age = args[1]
self._location = args[2]
except IndexError:
pass
e1 = Employees('stephen', 30, 'Bournemouth')
for key, value in e1.__dict__.items():
print('{}: {}'.format(key, value))
e1(_name='new', _location='new loc', _age='new_age')
for key, value in e1.__dict__.items():
print('{}: {}'.format(key, value))
#Console Output:
_name: stephen
_age: 30
_location: Bournemouth
_name: new
_age: new_age
_location: new loc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment