All three methods below can be used to modify the state of an object. What is best practice when needing to modify complex objects (containing multiple dicts and sub-objects) via functions?
class InnerStuff ( object ) :
def __init__(self):
self.x = 'Original x'
self.y = 'Original y'
self.z = 4.4
class Stuff ( object ) :
def __init__(self):
self.inner_stuff = InnerStuff()
def change_object(var):
var.inner_stuff.x = 'Changed x'
var.inner_stuff.y = 'Changed y'
var.inner_stuff.z = 3.2
def get_modified_object(var):
var.inner_stuff.x = 'Modified x'
var.inner_stuff.y = 'Modified y'
var.inner_stuff.z = 6.8
return var
def get_modified_variables(x, y, z):
x = x + ' then changed'
y = 'y is now different text'
z = z + 1.4
return x, y, z
stuff = Stuff()
print("Before change: {0}".format(vars(stuff.inner_stuff)))
change_object(stuff)
print("After change_object: {0}".format(vars(stuff.inner_stuff)))
stuff = get_modified_object(stuff)
print("After get_modified_object: {0}".format(vars(stuff.inner_stuff)))
stuff = Stuff()
stuff.inner_stuff.x, stuff.inner_stuff.y, stuff.inner_stuff.z = get_modified_variables(stuff.inner_stuff.x, stuff.inner_stuff.y, stuff.inner_stuff.z)
print("After get_modified_variables: {0}".format(vars(stuff.inner_stuff)))
selfas the first implicit argument and instead of Java I'd say in OOP in general.