Skip to content

Instantly share code, notes, and snippets.

@StephenFordham
Last active February 12, 2022 11:28
Show Gist options
  • Save StephenFordham/c4622ea89e6ddd48ae7fb87ecc810ced to your computer and use it in GitHub Desktop.
Save StephenFordham/c4622ea89e6ddd48ae7fb87ecc810ced to your computer and use it in GitHub Desktop.
Unusual ways to control attrbute access in Python, Example 1
class Employees(object):
def __init__(self, name, location, age):
self._name = name
self._age = age
self._location = location
# attribute re-assignment
def __call__(self, *args, **employee_info):
if hasattr(self, '_name') and len(args) == 1:
self._name = args[0]
elif hasattr(self, '_location') and len(employee_info) == 1:
self._location = employee_info['location']
else:
values = [value for key, value in self.__dict__.items()]
self.__init__(name=values[0], location=values[1], age=values[2])
emp = Employees('stephen', 'bournemouth', 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment