I am having a problem with default arguments in python classes. It seems that when an argument is not given, the default links to the same object for all instances of a class. Example:
class My_class:
def __init__(self,options=[]):
self.options = options
class1 = My_class()
class2 = My_class()
class2.options.append('something')
print(class1.options)
This would print:
['something']
How can i make sure each instance of a class will have a unique list for options, instead of a reference to the same object. For example, this is how i could do it:
def __init__(self,options=None):
if options is None:
options = []
self.options = options
However, this doesnt feel correct to me. So my questions are if there is a better way to do it, and for someone to explain the initial behaviour to me since i have a clue what is going on, but i don't fully understand why