Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python does not really distinguish "public" from "private" variables. Anything you assign to a name in self using 'self.something = value' will be an instance variable for that instance.</p> <p>Python does have two conventions that are relevant here:</p> <p>1) Python fields or methods are prefixed with underscores('_') to tell users 'this is not a concern outside this class'. A single underscore is a conventional way of identifying more or less what a language like C# would call a 'protected' variable: it's internal to the implementation and should be left alone except by this class and derived classes. A double underscore is like a C# 'private' variable: it's internal and should be left alone even by derived classes, it can't be overridden </p> <pre><code>class Example(object): def __init__(self) self._protected= "protected" self.__private = "private" self.public = "public" class Derived(Example): pass d = Derived() print d._protected &gt;&gt; protected print d.__private &gt;&gt; AttributeError: 'Derived' object has no attribute '__private' </code></pre> <p>Strictly speaking the single underscore is <em>just a typographical convention</em>. The double underscore does actually mangle the name inside the class to prevent inheritance.</p> <p>2) You example is potentially case for using the property decorator if AnotherClass is actually going to hang around, and if manipulating the AnotherClass instance affects the state of AnotherClass.</p> <pre><code>class C: def __init__(self, parent = None): super(C, self).__init__(parent) self._private = AnotherClass() @property def public(self): return self._private.some_method_on_AnotherClass() @public.setter def set_pub(self, val) self._private.set_value_on_AnotherClass(val) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload