I have one like this
class A: __a = 0 def __init__(self): A.__a = A.__a + 1 def a(self): return A.__a class B(A): def __init__(self): # how can I access / modify A.__a here? A.__a = A.__a + 1 # does not work def a(self): return A.__a
Can I access the class variable __ain B? Is it possible to write ainstead __a, is this the only way? (I think the answer can be quite short: yes :)
__a
B
a
, __a , . - , . , -, _<classname>__<variablename> __<variablename>. - __<variablename>, .
_<classname>__<variablename>
__<variablename>
, , , (a) , , (b) .
A._A__a. Python __, , _<class-name>, "private" . , A.__a, B, A._B__a:
A._A__a
__
_<class-name>
A.__a
A._B__a
>>> class Foo(object): _Bar__a = 42 ... >>> class Bar(object): a = Foo.__a ... >>> Bar.a 42
There are Python @staticmethodand decoders @classmethodthat can be used to declare a static or class method. This should help access the data element of the class:
@staticmethod
@classmethod
class MyClass: __a = 0 @staticmethod def getA(): return MyClass.__a class MyOtherClass: def DoSomething(self): print MyClass.getA() + 1
An example inspired by this source: http://www.rexx.com/~dkuhlman/python_101/python_101.html
Source: https://habr.com/ru/post/1750684/More articles:Creating a vertical scrollable parent div that does not cause overflow of children - cssWhat are the criteria for determining "required or not in a Postgresql query? - sqlGetting events when deleting and pasting into jquery text box - jqueryCharacter encoding - encodinghttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1750683/alternatives-to-propertyinfogetvalue-for-mono&usg=ALkJrhiW6ptCKGEy30RMbXwTl-Od6D5J5gThe difference between events with an event with SomeEvent (arg) and SomeEvent.Invoke (arg) - c #Collapse taxonomy in default node field - viewsRails: loss of quotes in message parameters - ruby-on-railsSilverlight DataGridTemplateColumn vs DataGridTextColumn - silverlightIs there a set of open source Netflow Collector C ++ libraries? - c ++All Articles