Timeline for Is it a good practice to declare instance variables as None in a class in Python?
Current License: CC BY-SA 4.0
23 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| S Nov 18, 2021 at 14:08 | history | edited | lennon310 | CC BY-SA 4.0 | editor could be misunderstood, IDE is more precise |
| S Nov 18, 2021 at 14:08 | history | suggested | CommunityBot | CC BY-SA 4.0 | editor could be misunderstood, IDE is more precise |
| Nov 18, 2021 at 13:04 | review | Suggested edits | |||
| S Nov 18, 2021 at 14:08 | |||||
| Apr 10, 2019 at 22:04 | comment | added | MattW | Actually self.name gets copied from the class variable, even if not explicitly declared, which is a little confusing. The class variable gets updated by calling <classname>.<varname> versus self.<varname>. Example: class test(): x=1 y=7 def __init__(self,x): self.x=x self.y=x*2 def instx(self): return self.x @classmethod def classx(cls): return test.x def gety(self): print (self.y) print (test.y) return self.y a=test(2) print(a.instx()) print(a.classx()) a.gety() Output: 2 1 4 7 | |
| May 27, 2018 at 9:15 | comment | added | jolvi | These assignments at class level have no effect on the rest of the code. They have no effect on self. Even if self.name or self.age were not assigned in __init__ they would not show up in the instance self, they only show up in the class Person. | |
| Apr 30, 2018 at 13:57 | comment | added | c z | "Never let your IDE dictate what code you write" is a debated issue. As of Python 3.6 there's in-line annotations and a typing module, which allow you to provide hints to the IDE and linter, if that sort of thing tickles your fancy... | |
| Apr 16, 2018 at 16:06 | comment | added | Bachsau | If you want to predefine the attributes your instance will use, add __slots__ to your class. | |
| Feb 12, 2018 at 19:20 | comment | added | dashesy | If it is just for autocompletion you can use type hinting, and the additional docstrings will be a plus too. | |
| Jul 13, 2015 at 12:24 | history | protected | gnat | ||
| Aug 27, 2014 at 19:32 | vote | accept | Remco Haszing | ||
| Aug 27, 2014 at 18:44 | answer | added | Jon Jay Obermark | timeline score: 0 | |
| S Aug 27, 2014 at 18:38 | history | suggested | Peter Mortensen | CC BY-SA 3.0 | Copy edited. |
| Aug 27, 2014 at 18:33 | review | Suggested edits | |||
| S Aug 27, 2014 at 18:38 | |||||
| Aug 27, 2014 at 17:39 | comment | added | Bakuriu | By the way: using a proper python IDE (e.g. PyCharm), setting the attributes in the __init__ already provides autocompletion etc. Also, using None prevents the IDE to infer a better type for the attribute, so it's better to use a sensible default instead (when possible). | |
| Aug 27, 2014 at 17:00 | history | tweeted | twitter.com/#!/StackProgrammer/status/504674811013267456 | ||
| Aug 27, 2014 at 15:14 | answer | added | 9000 | timeline score: 33 | |
| Aug 27, 2014 at 14:53 | answer | added | Weaver | timeline score: 89 | |
| Aug 27, 2014 at 14:38 | answer | added | Daenyth | timeline score: 11 | |
| Aug 27, 2014 at 13:37 | history | edited | Remco Haszing | CC BY-SA 3.0 | deleted 46 characters in body |
| Aug 27, 2014 at 12:08 | answer | added | this.myself | timeline score: 20 | |
| Aug 27, 2014 at 11:24 | comment | added | Martijn Pieters | Never let your IDE dictate what code you write? | |
| Aug 27, 2014 at 11:11 | review | First posts | |||
| Aug 27, 2014 at 11:22 | |||||
| Aug 27, 2014 at 11:08 | history | asked | Remco Haszing | CC BY-SA 3.0 |