[TriPython] Private variables and methods.

Luis Valencia livalencia at my.waketech.edu
Wed Apr 18 20:45:48 EDT 2018


Hello all,


I'm having a bit of difficulty with the concept of private variables and methods. I was looking at StackOverflow and I came upon this https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes

[https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded]<https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes>

class - Does Python have “private” variables in classes ...<https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes>
stackoverflow.com
I'm coming from the Java world and reading Bruce Eckels' Python 3 Patterns, Recipes and Idioms. While reading about classes, it goes on to say that in Python there is no need to declare instance

Is this answer still acceptable? The reason why I'm ask is, since I'm taking a Python class some of the reading assignment says that Python "allows us to block access to any instance variable or method if we want to." But I think SO is telling me something else.

Here's an excerpt from the lesson:

By default, all instance variables and methods of an object are publicly accessible.  That means client code (e.g. code in the main module) can access any instance variable and call any method of the object.  Sometimes it may be a bad idea to make class members, especially instance variables, that wide open.  Python allows us to block the access to any instance variable or method if we want to.  In fact, it is a convention to make all instance variables private to protect their integrity.




Thank you,
Luis
-------------- next part --------------
   Hello all,

   I'm having a bit of difficulty with the concept of private variables and
   methods. I was looking at StackOverflow and I came upon
   this [1]https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes

   [2][IMG] [3]class - Does Python have "private" variables in classes ...
            stackoverflow.com
            I'm coming from the Java world and reading Bruce Eckels' Python 3
            Patterns, Recipes and Idioms. While reading about classes, it
            goes on to say that in Python there is no need to declare
            instance

   Is this answer still acceptable? The reason why I'm ask is, since I'm
   taking a Python class some of the reading assignment says that Python
   "allows us to block access to any instance variable or method if we want
   to." But I think SO is telling me something else.

   Here's an excerpt from the lesson:

   By default, all instance variables and methods of an object are publicly
   accessible.  That means client code (e.g. code in the main module) can
   access any instance variable and call any method of the object.  Sometimes
   it may be a bad idea to make class members, especially instance variables,
   that wide open.  Python allows us to block the access to any instance
   variable or method if we want to.  In fact, it is a convention to make all
   instance variables private to protect their integrity.



   Thank you,
   Luis

References

   Visible links
   1. https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes
   2. https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes
   3. https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes


More information about the TriZPUG mailing list