I'm trying to create a class with a self
attribute which is a list of all arguments contained in __init__
.
For example:
class thing:
def __init__(self,x,y=None,fav_animal=None):
self.x = x
if y is None:
self.y = None
else:
self.y = y
if y is None:
self.fav_animal = None
else:
self.fav_animal = fav_animal
self.arg_list = 'some code here?'
def args(self):
print(self.arg_list)
I'm trying to come up with a solution whereby the function args(self)
will return a list of the attribute names, ie [x, y, fav_animal]
automatically. I can just set self.arg_list = [x, y, fav_animal]
manually, but I'd like it to happen automatically whenever __init__
is called.
If I do it through list appending, eg:
class thing:
def __init__(self,x,y=None,fav_animal=None):
arg_list = []
self.x = x
arg_list.append(self.x)
# etc...
self.arg_list = arg_list
The function will return the values given to the arguments, rather than the names, as desired.
For context, the reason I want to do this is because I'll be using this type of thing in multiple projects, and I want to be able to add arguments to the __init__
without having to update the list manually.
Many thanks!