-2

Take the following example:

class User:
    name = ""
    def __init__(self, name):
        self.name = name

What is the point of declaring name = "" if you have to specify name anyway when you construct it?

robertspierre
  • 3,218
  • 2
  • 31
  • 46
  • 3
    That's pointless, as written. If there was no assignment to `self.name`, it would be a *class attribute*, shared by all instances. – jasonharper Jan 18 '22 at 23:24
  • The one directly in the class is a class attribute. – khelwood Jan 18 '22 at 23:25
  • Read about class variable vs instance variable. – David Jan 18 '22 at 23:25
  • There is no point . You don’t need that – eshirvana Jan 18 '22 at 23:25
  • It isn't a declaration. But also, this isn't a discussion forum, so we don't take subjective questions about why you should or shouldn't do something, without objective standards. The question might be answerable as "what does this line of code accomplish?", but presumably you already understand that. (But just to make sure: [there are no declarations in Python](https://stackoverflow.com/questions/11007627/python-variable-declaration/11008311#11008311). Anyway, the proper way to answer "why is the code like that?" is to *ask the author*. – Karl Knechtel Jan 18 '22 at 23:27
  • 1
    That said: many people with a background in other programming languages will mistakenly write this sort of thing, and it's usually harmless, so they don't learn the lesson. – Karl Knechtel Jan 18 '22 at 23:28

1 Answers1

0

You'd have to ask the person that did it, since that is a very broad question. Maybe git blame will help.

In general there is no point. They're different things: class attribute and instance attribute. One would be data associated with the type of object, the other with individual instances of the object.

theherk
  • 6,954
  • 3
  • 27
  • 52