0

When declaring your class instance variables, can you use a call to one of the class' methods to initialize one of the instance variables in your constructor? For example, let's say I had a Button class, and a method in that class methodA() that set a variable called 'active' to False. Could I initialize 'active' by a call to the method?

class Button:
    def __init__(self):
        *snip*
        self.methodA()

    def methodA(self):
        self.active = False

Would this technique be correct for initializing my instance variable 'active'?

2
  • 4
    Yes, you can, but why would you do this instead of setting self.active = False in the init method? Commented Apr 10, 2020 at 17:54
  • That's a great question - I am working out of the Zelle Python Programming 3rd ed. textbook, and this is the way he has it set up. He is covering a cannonball firing project, and initializes the boolean instance variable for a button state named 'active' by calling the deactivate() method so any button will have an initial value for 'active' = False. The deactivate() method does do some other things, namely it adds on some more formatting of color and line width for some other instance variables that while initialized, are accentuated further by the deactivate() method. Commented Apr 10, 2020 at 20:36

2 Answers 2

1

Yes, that may be done. In fact, in Python, any non-static method can initialize new data members for the objects of a class. However, it is recommended to always initialize the members in __init__(), because it is considered to be a good practice to initialize the to-be used members upon object-creation and initialization.

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

Comments

0

With the context added in your comment, your question makes more sense. Yes, you can set any attribute of an object in another method. Actually, due to Python's dynamic typing, you can set them even after your object has been initialized:

class Example:
    pass

e = Example()
e.foo = 'bar'
e.foo  # returns 'bar'

Generally, however, you want to initialize all attributes of an object in its __init__ method, because it's easier to maintain, and you always know where to look when you don't remember whether a particular attribute is defined or where it came from.

In your example, it makes more sense to initialize your class with all attributes it will ever have, and then call methods to modify them. For example:

class Button:
    def __init__(self):
        *snip*
        self.active = False
        self.color = 'red'
        self.linewidth = 2

    def activate(self):
        self.active = True
        self.color = 'green'
        self.linewidth = 5

    def deactivate(self):
        self.active = False
        self.color = 'red'
        self.linewidth = 2

Now you probably noticed that we have code duplication - the __init__ method basically copies the lines in the deactivate method! Still, it's great for readability to have all attributes in the __init__ method. What do we do?

Consider this compromise: initialize all attributes with None values, and then call the deactivate method:

 class Button:
    def __init__(self):
        *snip*
        self.active = None
        self.color = None
        self.linewidth = None
        self.deactivate()

    def activate(self):
        self.active = True
        self.color = 'green'
        self.linewidth = 5

    def deactivate(self):
        self.active = False
        self.color = 'red'
        self.linewidth = 2

This combines the best of both worlds: our code is readable (the __init__ method basically reads "initialize a button with these attributes and then deactivate it"), but also suffers from no code duplication.

This question adds some further insights on why it's preferable to create all attributes in the __init__ method.

Comments

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.