10

In python when you initialize an instance variable (e.g. self.my_var) you should do it in your class __init__ function, so that the memory is properly reserved for this variable per instance (<--my mistake, see bellow). When you want to define class level variables you do it outside of a function and without the self prefix.

What happens when you instantiate a variable inside a function other than the __init__ with the self prefix? It behaves like a normal instance variable, is there a compelling reason to not do it? other than the danger of making code logic implicit, which is enough of a reason already, but I am wondering are you potentially running on memory or other hidden issues if you do so?

I couldn't not find that discussed somewhere.

update sorry

I misinterpreted some answers including the first and the third here Python __init__ and self what do they do? (looking for the others) and thought that __init__ is some special type of function, thinking that it somehow has memory allocation functionality (!?). Wrong question.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
LetsPlayYahtzee
  • 7,161
  • 12
  • 41
  • 65
  • You said: "so that the memory is properly reserved". No. But it's generally good style to initialise all attributes in `__init__`, since it makes it easier for readers to know what attributes your class has. But if you can't initialise those attributes to sensible values it may be better for your code to fail noisily rather than to do the wrong thing with some "placeholder" initial value like `None` or some other false-ish value. – PM 2Ring Aug 03 '17 at 14:10
  • 1
    Python is not C, and generally it is _not_ fruitful to try to understand the Python data model in terms of the C data model. Please see [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables). Also see [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder, for more in-depth info on this topic. – PM 2Ring Aug 03 '17 at 14:11
  • Well, `__init__` _is_ a special method; the double underscores at the start and end of the name indicate that it's special. And it is special in that it gets called automatically to perform initialization of the fresh instance returned by `__new__`. But ultimately it's only a convention that we use it to initialize the instance's attributes, any method is permitted to do that, and as the answers below show, it's also perfectly legal to add instance attributes via code outside the class definition. – PM 2Ring Aug 03 '17 at 14:17
  • see my update, got confused, mainly by the third answer in the link. I quote "__init__ gets called when memory for the object is allocated". – LetsPlayYahtzee Aug 03 '17 at 14:20

4 Answers4

12

The __init__ method is not special. The only thing that makes __init__ interesting is the fact that it gets called when you call MyClass().

The following are equivalent:

# Set inside __init__
class MyClassA:
    def __init__(self):
        self.x = 0
obj = MyClassA()

# Set inside other method
class MyClassB:
    def my_initialize(self):
        self.x = 0
obj = MyClassB()
obj.my_initialize()

# Set from outside any method, no self
class MyClassC:
    pass
obj = MyClassC()
obj.x = 0

What makes an instance variable is when you assign it, and that can happen anywhere. Also note that self is not special either, it's just an ordinary function parameter (and in fact, you can name it something other than self).

so that the memory is properly reserved for this variable per instance.

You do not need to "reserve memory" in Python. With ordinary object instances, when you assign self.x = 0 or obj.x = 0, it is kind of like putting a value in a dictionary. In fact,

# This is sometimes equivalent, depending on how obj is defined
obj.__dict__['x'] = 0
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
2

This has nothing to do with "reserving memory". Python is not C; you should absolutely not think in terms of memory allocation when writing Python.

As a dynamic language, Python behaves in exactly the same way wherever you set an attribute, and __init__ is not in any way privileged when it comes to creating attributes.

The main reason for doing it there, though, is that it then gives your class a consistent interface. If you don't assign at least a placeholder when creating an instance, any code that accesses that attribute needs to check whether or not it even exists first.

That said, there are still plenty of use cases where dynamically annotating an attribute is useful and perfectly acceptable.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • as of [PEP 412](https://www.python.org/dev/peps/pep-0412/) don't the `__dict__` of classes use key sharing to reduce memory? My understanding is this is based on the attributes defined in the `__init__` method, but happy to be corrected. – jeremycg Aug 03 '17 at 14:07
  • 1
    I'm not sure pep412 made it's way into CPython (at least it seems not to be in 3.4.3), but anyway: nothing in pep412 mentions such a drastic and backward incompatible change. – bruno desthuilliers Aug 03 '17 at 14:19
  • I'm basing this off Brandon Rhodes talk at Pycon [here](https://youtu.be/66P5FMkWoVU?t=21m16s), so I think it made it in 3.3+ – jeremycg Aug 03 '17 at 14:23
0

I was looking for an answer for this Question specifically as i defined a global variable with self inside a method instead of __init__ method, and it didn't affect my code. But when i needed to use that variable in another method -before calling the method that initiated the variable-, it didn't work. Obviously, the variable wasn't defined yet! So, the answer would be that we define all global variables in the __init__ method, because it gets executed once you create an instance from your class. Thus, defining all the variables from the beginning and use them whenever you want in any coming method.
initializing a global variable withing a normal method, will force you to execute this method first before using that variable in any further way.

-1

__init__ is kind of a constructor to the python classes, when you define a variable with self, like self.abc it becomes an object attribute that can be accessed with self or the object. It is initialized at the time of object creation itself.

When you define a variable outside of the __init__ or any functions inside the class without self it becomes a static attribute for that class. and remains same for all instances for that class.

If you define it within other member functions of the class with self, it is very much similar to what you do within __init__ it just not automatically get called while instantiation, but you have to call it explicitly

binu.py
  • 1,137
  • 2
  • 9
  • 20
  • To be pedantic, `__new__` is the constructor, `__init__` is the initializer. But it _is_ rather common for people to call `__init__` the constructor when they're speaking casually. – PM 2Ring Aug 03 '17 at 14:13