0

I'm relatively new to python and I'm working on a script that uses class objects extensively. I remember from my C++ lessons years ago that C++ class objects have public and private variables, where public variables can be accessed like MyClass.myVariable = #, but private variables have to be set using a predefined function, ie, MyClass.setMyVariable(#). From what I remember, this allowed you to ensure that variables were of the right type and prevented misuse or corruption.

Is there something equivalent in python? For a class-intensive script, should I bother to make internal class functions that set and pass the variables contained in the class object, or should I just set and access the variables directly?

I'm using python 3 on linux Mint. The code is ultimately for an Android app.

CMB
  • 133
  • 4
  • that's a subjective question. but python has the built-in [`property`](https://docs.python.org/3/library/functions.html#property), so it's clearly made for using – bobrobbob May 26 '18 at 10:59
  • There's some good discussion of this here: https://stackoverflow.com/a/36943813/9794932 and in the other answers. I concur with the answer I linked: start by just accessing the variables directly, then introduce properties if you need more functionality. – Rob Bricheno May 26 '18 at 11:10
  • 1
    Don't try to write C++ in python. – cdarke May 26 '18 at 11:27
  • Gotcha. So just access variables directly unless you need to ensure they meet some requirement before storing, for which you use the @property code? – CMB May 27 '18 at 09:59

0 Answers0