Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding __init__ in Python [closed]

Tags:

python

oop

I am working on a Python file I received and I see a Class with many kinds of variables when it comes to initialization in Python. It is not clear to me what logic I should follow when I define attributes and which ones to initialize or not, below part of the code:

class Simulator(object):
    colors = {'gray'    : (155, 155, 155)}

    def __init__(self, env, size=None, update_delay=2.0, display=True):
        self.env = env
        self.size = size if size is not None else self.env.grid_size[0]
        self.road_width = 44
        self.bg_color = self.colors['gray']

        self.quit = False
        self.start_time = None
        self.current_time = 0.0
        if self.display:
            try:
                self.pygame = importlib.import_module('pygame')
                self.pygame.init()
                self.screen = self.pygame.display.set_mode(self.size)

For example:

  • env : it is declared in the parentheses and later on initialized --> Clear

  • size: it is declared but when initialized it uses self.env.grid_size[0]. How env can have an attribute grid_size that it was not defined or initialized?

  • road_with: Why it is not mentioned in the parentheses? I would have just put road_with in the parentheses with the default value 44

  • bg_color: Why it is not defined in the parentheses?

  • Why quit, start_time, current_time are not defined in the parentheses?

  • How it works to declare but not initialize a self.pygame which is related to a module which is not previously called in the code?

  • How it works to init another module with the self.pygame.init()?

like image 267
Mariano Avatar asked Jan 21 '26 15:01

Mariano


2 Answers

size: it is declared but when initialized it uses self.env.grid_size[0]. How env can have an attribute grid_size that it was not defined or initialized?

In the line before, self.env is set as the first positional argument the user supplies to the initializer when creating an instance of Simulator. It is up to the user to supply an object which has an attribute grid_size. (This should probably be mentioned by a doc-string or a type hint.)

road_with: Why it is not mentioned in the parentheses? I would have just put road_with in the parentheses with the default value 44

Only let the user know what he must know about the inner workings of an object. It's the principle of information hiding. If every instance of our object should carry the attribute road_width with the value 44, why bother the user with another optional argument where it's not clear what calling the initializer with other values for road_width would even do?

bg_color: Why it is not defined in the parentheses?
Why quit, start_time, current_time are not defined in the parentheses?

see above

I don't understand your last two questions.

like image 124
timgeb Avatar answered Jan 23 '26 06:01

timgeb


The main question here seems to be: Why are some attributes that are initialized in __init__ not also parameters to __init__? The answer is: Because you don't want the user to be able to set those.

For example, those could represent some internal state of the object. Think of an Iterator class. The parameter could be a list, but its internal state is also defined by the current index in that list. That index could be initialized in __init__ to 0, and it does not make sense to pass it as a parameter. This seems to apply to your quit, start_time and current_time attributes`.

Or maybe some attributes are derived from others, e.g. you could have a Rectangle class, with parameters width and height, and another attribute area, which is derived from those two parameters. This seems to be the case e.g. for the self.screen attribute.

And some attributes may just be constant and unchangeable, as seems the case with road_width and color. Those could maybe be moved outside of the instance, if they are the same for each instance of the class anyway.

like image 35
tobias_k Avatar answered Jan 23 '26 06:01

tobias_k