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
bar!!!
bar
How do you call a class' method in init?
Modify self.z
, not inp
:
def my_function(self, inp):
self.z += '!!!'
Secondly strings are immutable in python, so modifying inp
won't affect the original string object.
See what happens when self.z
is 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:
['!', '!', '!']
['!', '!', '!']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With