Difference between two types of class property definition?

Is there a difference between

class Foo(object): bar = 1 def __init__(self): ... etc. 

and

 class Foo(object): def __init__(self): ... etc. Foo.bar = 1 

In both cases, bar is a property of the class, and it is the same for all instances of the class, right?

+4
source share
3 answers

I would say that the only difference is that in the second case, Foo.bar does not exist until the operator Foo.bar = 1 is executed, and in the first case it is already available when the class object is created.

This is probably a small difference without any effect in your code (unless there is code that requires Foo.bar before it is available in the second case). However, I would say that the first option is better in terms of readability, since you do not need to scroll down to know the attributes for your class, they already exist.

+4
source

If you are writing your own code, follow the link:

 class Foo(object): bar = 1 

Become this version:

 class Foo(object): pass Foo.bar = 1 

Despite the fact that he is legal

  • Looks like a hack.
  • In most cases, it is less readable.
  • You may have problems if you try to access the bar attribute before creating it:

     >>> class Foo: ... pass ... >>> f = Foo() >>> f.bar Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Foo' object has no attribute 'bar' >>> Foo.bar = 1 >>> f.bar # but hey, now you're ok! 1 

Other than that, I do not see many differences.

+4
source

Both versions are the same (see transcript of tests at the end of this answer), but note that (a) in no case is bar same for all instances of Foo .

An instance is currently being created, when reading bar in the instance, the value in Foo will be read, and the assignments of Foo.bar will be displayed in the instance until instance.bar is assigned. At this point, the destination instance has its own entry __dict__ , which is completely independent of the class.

 In [62]: class Foo: pass In [63]: Foo.bar = 1 In [64]: Foo.bar Out[64]: 1 In [65]: f = Foo() In [66]: f.bar Out[66]: 1 In [67]: f.bar +=1 In [68]: f.bar Out[68]: 2 In [69]: Foo.bar Out[69]: 1 In [70]: Foo.bar +=3 In [71]: Foo.bar Out[71]: 4 In [72]: g = Foo() In [73]: g.bar Out[73]: 4 In [74]: class Qux: bar = 1 In [75]: Qux.bar Out[75]: 1 In [76]: q = Qux() In [77]: q.bar Out[77]: 1 In [78]: q.bar+=1 In [79]: q.bar Out[79]: 2 In [80]: Qux.bar Out[80]: 1 In [81]: Qux.bar +=1 In [82]: r = Qux() In [83]: r.bar Out[83]: 2 In [84]: q.bar Out[84]: 2 In [85]: s = Qux() In [87]: s.__dict__ Out[87]: {} In [88]: q.__dict__ Out[88]: {'bar': 2} In [89]: Qux.bar = 'foo' In [90]: Qux.bar Out[90]: 'foo' In [91]: s.bar Out[91]: 'foo' 
-one
source

Source: https://habr.com/ru/post/1396794/


All Articles