4

This Question / Answer (Python call constructor in a member function) says it is possible to to call the constructor from within a member function.

How do I do that?

Is it good a style?

I tried it with the following code:

class SomeClass(object):
    def __init__(self, field):
        self.field = field

    def build_new(self):
        self = SomeClass(True)

def main():
    inst = SomeClass(False)
    inst.build_new()
    print(inst.field)

if __name__ == '__main__':
    main()

As output I get: False

Since I called the build_new() method inst.field should be True or not?

Community
  • 1
  • 1
jonie83
  • 1,136
  • 2
  • 17
  • 28
  • possible duplicate of [Python call constructor in a member function](http://stackoverflow.com/questions/8343576/python-call-constructor-in-a-member-function) – Ian Stapleton Cordasco Aug 04 '14 at 12:36
  • 1
    `self` is just a local variable within the instance method. Assigning to it changes the local variable, it doesn't replace the object itself. Related: http://stackoverflow.com/questions/1216356/is-it-safe-to-replace-a-self-object-by-another-object-of-the-same-type-in-a-meth – Wooble Aug 04 '14 at 12:38

2 Answers2

13

The problem is not in calling the constructor, but what you're doing with the result. self is just a local variable: assigning to it won't change anything at all about the current instance, it will just rebind the name to point to a new instance which is then discarded at the end of the method.

I'm not totally certain what you are trying to do, but perhaps you want a classmethod?

class SomeClass(object):
   ...
   @classmethod
   def build_new(cls):
       return cls(True)



SomeClass.build_new(False)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • How does this work? `build_new` takes one argument, but it is being passed two. Where does this `False` go? – Harry Jul 15 '23 at 07:54
8

I believe what you are looking for is just calling the init function again.

class SomeClass(object):
    def __init__(self, field):
        self.field = field

    def build_new(self):
        self.__init__(True)

This will cause the field variable to be set to True over False. Basically, you are re-initializing the instance rather than creating a brand new one.

Your current code creates a new instance and just loses the reference to it when it goes out of scope (i.e. the function returning) because you are just rebinding the name of self to a different value not actually changing the inner contents of self.

rdp
  • 2,675
  • 2
  • 21
  • 21