-1

I have the following snippet of code:

Employees1 = []
Emp_ID1 = 100

class Employee:
   ID = None
   FirstName = None
   LastName = None
   Salary = None

   def __init__ (self, fn, ln, sal):
       global Emp_ID1
       Emp_ID1 += 1 # What's this?
       self.ID = Emp_ID1
       self.FirstName = fn
       self.LastName = ln
       self.Salary = sal
       Employees1.append(self)

My question here is: what is the scope of the Employees1 list? I see it's defined outside function but it runs fine. So is Python treating this as a global variable? So we don't have to mention the keyword global before a variable?

If I go by this assumption, then I have this snippet again:

value_789 = 0

class Self_Assign():
    def assign():
        value_789 += 1
        print(value_789)

def assign():
    value_789 += 1
    print(value_789)

When I run this, it says local variable value_789 referenced beore assignment. Why is it saying so? Why isn't it taking this as a global variable and running fine like before?

Also, when I run this code snippet like

obj = Self_Assign()
print(obj.assign)

I cannot see any print done, it treats this assign as a method and says <bound method Self_Assign.assign of <__main__.Self_Assign object at 0x7fca74fed160>>.

I do have a function assign() defined outside class. How does Python differentiate between a method and a function? When I try to run the method assign using assign() it says the same error local variable referenced....

If Python treats value_789 as a global variable it should've understood it like what it did with the Employee1 list.

I read the article What's the difference between a method and a function? but still feel confused about how Python accesses a method and a function. What is the key difference between a method and a function? I have programmed in Java as well which too is OOP but is there any method concept there? All that we do are to define our own functions or use the inbuilt ones.

martineau
  • 119,623
  • 25
  • 170
  • 301
QUEEN
  • 383
  • 1
  • 5
  • 13
  • The only time you need to declare variables as `global` is in functions to prevent them from being assumed to be local, which is the default. From the way your `Employee` class is written, it also looks like you don't understand how class attributes work with respect to defining attributes, so I would suggest studying that topic more rather than asking about it here because Stack Overflow is not intended to replace existing tutorials or documentation. – martineau Feb 16 '22 at 19:57

1 Answers1

1

You have global variables. The problem is that the += assignment makes value_789 a local variable in assign, rather than modifying the global variable of the same name. You need to explicitly make value_789 refer to the variable in the global scope with a global statement:

def assign():
    global value_789
    value_789 += 1

The difference with Employees1 is that you are not assigning a new value to the name; you are just accessing a mutating method with the object bound to the global name.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you so much. I `__init__()` is a constructor but what do you mean by a mutating method? Also, from the `Employee` class is `Employees1` considered as a global variable that gets modified inside the object? – QUEEN Feb 26 '22 at 08:19
  • 1
    `__init__` is an *initializer*; it's called after the actual constructor (`__new__`). A mutating method is one that changes the object in some way. The `+=` operator is implemented by the mutating method `__iadd__` if it is defined; otherwise, it's implemented using the non-mutating method `__add__`. The type `int` does not define `__iadd__`. – chepner Feb 26 '22 at 13:35