Answer the question
In order to leave comments, you need to log in
Why assign a class variable to itself?
Good afternoon, habravchane!
Recently I have been looking through different libraries and in many I meet this:
class Foo:
boo = dict
def __init__(self):
self.boo = self.boo
Answer the question
In order to leave comments, you need to log in
Alternatively, because boo is a class field. The line self.boo = self.boo assigns a field to an instance. I don't know why this might be necessary in practice, but here's an example to reproduce when it affects something:
class A:
factory = dict
def method(self):
return self.factory()
a = A()
A.factory = list
print(a.method()) # []
class B:
factory = dict
def __init__(self):
self.factory = self.factory
def method(self):
return self.factory()
b = B()
B.factory = list
print(b.method()) # всё равно словарь: {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question