0

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)))
5
  • 4
    None of them is best practice. A class method should be used to modify an object. Commented Dec 26, 2015 at 3:42
  • @Klaus D. do you mean putting thin in the InnerStuff class: (at)classmethod def set_x(new_value): InnerStuff.x = new_value eg you would in Java? Commented Dec 26, 2015 at 4:09
  • Basically, except you don't need the decorator but self as the first implicit argument and instead of Java I'd say in OOP in general. Commented Dec 26, 2015 at 4:19
  • @Klaus D. How is that different from say the change_object function - we are still modifying the internals of the class if we reach in directly as per change_object or accessing through a set method? I guess my question is more related to should an object be modified inside a function or should values from the function be operated on then set for the function (eg get_modified_variables)? Commented Dec 26, 2015 at 4:27
  • 1
    Sorry, but a discussion about the very basical concepts of OOP does not lead anywhere and therefor is it out of the scope of SO. Commented Dec 26, 2015 at 4:32

1 Answer 1

3

The most idiomatic would be an instance method that mutates and returns the default None. However, there are people who like mutation methods to return the object also so they can chain them.

In your particular example, there might be a question as to which class should get the method and if the inner class, whether the method should be called directly or via an output method.

class InnerStuff ( object ) :
    def __init__(self):
        self.x = 'Original x'
        self.y = 'Original y'
        self.z = 4.4
    def change_object(self): # Option A
        self.inner_stuff.x = 'Changed x'
        self.inner_stuff.y = 'Changed y'
        self.inner_stuff.z = 3.2

class Stuff ( object ) :
    def __init__(self):
        self.inner_stuff = InnerStuff()

    def change_object(self): # Option B
        self.inner_stuff.x = 'Changed x'
        self.inner_stuff.y = 'Changed y'
        self.inner_stuff.z = 3.2

    def change_object(self): # Option C if chose option A
        self.inner_stuff.change_object()

The generality and vagueness of the question and the necessary vagueness of the answer is likely the reason for the downvote.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed, my question is probably not specific enough. I am a bit wary of modifier type functions but have been using them as sometimes they make getting something done easier. Adding instance methods to the objects in question (without chaining) is probably a good way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.