0

I got this example here, and I think it might be a good example for my question:

class Employee:
    def __init__(self ,first ,last ,pay ):
        entry_gate = 10;
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first+"."+last+"@company.com"

I have noticed that in some __init__ functions, there are variables that don't have a self. in front of the variables. I understand all the self. variables should be instance variables. Then, how about the variables that don't have a self. within the constructor? Should I understand it as a class variable because it doesn't point to any instance? And is there a difference between class constructor and instance constructor if they both use __init__ as the function name?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Linda
  • 37
  • 5
  • 6
    They're just local variables. `entry_gate` in this example doesn't do *anything*. – jonrsharpe Dec 08 '20 at 20:30
  • Does this answer your question? [correct way to define class variables in Python](https://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python) – mkrieger1 Dec 08 '20 at 20:37
  • 1
    `__init__` is the initializer, not a constructor. I'm not just being pedantic; the instance as already been *created* by the time `__init__` is called. `__init__` simply does stuff with or to the new instance (which is bound to the parameter `self`). `entry_gate` is a local variable, no different from a local variable in any other function you might define. – chepner Dec 08 '20 at 20:40

1 Answers1

0

The __init__ function in a class is simply the first function that is called when that class is initialized, hence the function name. There isn't any special property of variables defined in the __init__ function, except for being defined first. In this case, the __init__ function is defining a local variable entry_gate and assigning it a value of 10. This is similar to:

def foo():
    entry_gate = 10

Within a class, this structure stays the same, with the addition that any self.xxx variables are tied to the instance of the class (defined by self) while non-self variables are not.

In terms of the semicolon, there isn't much of a reason for it to exist in this code snippet. Semicolons can be used to denote a separation of statements, but they aren't required. In this instance, the semicolon has the effect of:

def __init__(self, first, last, pay):
    entry_gate = 10; self.first = first
    ...

For a more in-depth look at semicolons, I would point you to this question.

bbnumber2
  • 643
  • 4
  • 18
  • Then, what is the difference between self.entry_gate = 10 and entry_gate = 10 since they both belong to the instance? – Linda Dec 08 '20 at 21:43
  • @Linda They don't belong to the same instance. `self.entry_gate` belongs to the class instance and `entry_gate` belongs to the function instance. `self.entry_gate` can be referenced by this instance outside of the class, because it is within the class scope, while `entry_gate` cannot, because it's within local scope. – bbnumber2 Dec 08 '20 at 21:52