Consider the following class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
My coworkers tend to define it like this:
class Person:
name = None
age = None
def __init__(self, name, age):
self.name = name
self.age = age
The main reason for this is that their IDE of choice shows the properties for autocompletion.
Personally, I dislike the latter one, because it makes no sense that a class has those properties set to None
.
Which one would be better practice and for what reasons?
Best Answer
I call the latter bad practice under the "this does not do what you think it does" rule.
Your coworker's position can be rewritten as: "I am going to create a bunch of class-static quasi-global variables which are never accessed, but which do take up space in the various class's namespace tables (
__dict__
), just to make my IDE do something."