Double Underscore (Super Private) Method Call Python

Python Call Super Private

Probuse Admin

 

Error Example With Super Private Call.

>>> class MyClass():
...     def __init__(self):
...             self.__superprivate = "Hello"
...             self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}

How to Fix it ?

See Below


In Python:

1. Single underscore specify variable or method as semi private method which we can call using object.

2. Double underscore specify variable or method as super private method which we can not call simply using "object.__method name()"

=======================================

To call super private method in python we have to use syntax like below:

"object._CLASSNAME__method name()"...

======================

For example in Odoo:

_account_account test = account_obj._account_account__compute(self.cr, self.uid, account_ids, field_names=['credit', 'debit', 'balance'],context={}, query='l.analytic_account_id = 3') # Here account-account is class name and --compute is method name which is super private in class.