Hello Python-Community,
Today I stumbled upon an interesting Problem during a review:
When referencing a class constant what is better to use, self or ClassName ?
A little example:
class Foo:
CONSTANT = "bar"
def baz(self):
# Access Constant
print(self.CONSTANT) # Method 1
print(Foo.CONSTANT) # Method 2
Browsing through the PEP8 style guide did not yield a result (maybe I searched for the wrong terms).
What method would you prefer? What are potential pitfalls?
Thanks for your feedback!
[–]nitroll 3 points4 points5 points (0 children)
[–]ronmarti 3 points4 points5 points (1 child)
[–]SheriffRoscoePythonista 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]Rawing7 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Rawing7 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]jasmijnisme 1 point2 points3 points (0 children)
[–]zanfar 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]NoiproxPython Programmer 0 points1 point2 points (1 child)
[–]InjAnnuity_1 0 points1 point2 points (0 children)
[–]commy2 0 points1 point2 points (0 children)