code:
class c: def __init__(self, **kwargs): self.w = 'foo' self.z = kwargs['z'] self.my_function(self.z) def my_function(self, inp): inp += '!!!' input_args = {} input_args['z'] = 'bar' c = c(**input_args) print c.z
expected result
bar!!!
actual result
bar
how call class' method in init?
modify self.z
, not inp
:
def my_function(self, inp): self.z += '!!!'
secondly strings immutable in python, modifying inp
won't affect original string object.
see happens when self.z
mutable object:
class c: def __init__(self, ): self.z = [] self.my_function(self.z) def my_function(self, inp): inp += '!!!' print inp print self.z c()
output:
['!', '!', '!'] ['!', '!', '!']
Comments
Post a Comment