'printing data member using class name [closed]

class Acc:
    id=121
    def _init_(self,id):
        self.id=id;
acc=Acc(111) 
print(Acc.id)

When I try to run this code , the code runs fine and gives 121 as output when I directly call the data member 'id' using class name Acc and remove the object creation line from the code but the code gives error "TypeError: Acc() takes no arguments" when I include the second last line i.e. the object creation line in my code.



Solution 1:[1]

You need to use double underscores when creating constructor method:

...
def __init__(self, id):
...

In your question code you just created a method _init_.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakub Szlaur