You are not logged in.
Pages: 1
I've been programming in C++ for some years and decided to give Python a try. I read the Python documentation and came across private class members, which doesn't exist in Python(!?). I read that mostly this is overcome by naming the members like "__member" which seem like a messy solution. I'm used to encapsulate what's not supposed to be touched by the user, but apparently this is not how to work in python.
My question is how do you work with classes in Python and do you use private class members?
A typical example would be something like this:
class ClassA
{
public:
setEmail(string newEmail);
private:
string email;
}
When the user uses this class, he doesnt have access to the private variable, email, because I want to make sure the email is valid first, which is checked in setEmail(). How would I do this in python?
Offline
Check out this link -> http://docs.python.org/tut/node11.html# … 0000000000
There isn't really support for private data members in Python, I guess you are supposed to trust the user to use your setEmail() function.
Good luck!
Offline
Check out this link -> http://docs.python.org/tut/node11.html# … 0000000000
Thanks. That was the page about private members I read actually.
There isn't really support for private data members in Python, I guess you are supposed to trust the user to use your setEmail() function.
Good luck!
Haha. Trust the person who's using the library? Is this a choice from the python devs or are the private/protected stuff coming later?
It's not really a matter of trust, but simplification. The reason to make the "helper" functions (like the setEmail example above) is to make it easier for the people that uses the library, or whatever, later. There are other examples where it's crucial not to touch "hidden" variables/functions.
Since encapsulation is one of the fundamentals in object oriented programming, this affects the use for Python while coding OO. Very disappointing.
Offline
I also found this message on the python mailing list -> http://mail.python.org/pipermail/python … 68694.html
Thats what the "__whatever" thing is for. It tells people that they shouldn't mess with that variable or method.
Here is what seems to be a good discussion about this topic -> http://discuss.joelonsoftware.com/defau … .426661.12
EDIT: The first two replies seem really good.
Last edited by tom5760 (2007-04-26 17:25:53)
Offline
I also found this message on the python mailing list -> http://mail.python.org/pipermail/python … 68694.html
Thats what the "__whatever" thing is for. It tells people that they shouldn't mess with that variable or method.
Here is what seems to be a good discussion about this topic -> http://discuss.joelonsoftware.com/defau … .426661.12
EDIT: The first two replies seem really good.
Nice find. It gave me some of the answers I wanted. I also followed a link to http://www.mindview.net/WebLog/log-0025 by Bruce Eckel which was great as well. I guess I just have to accept it, afterall, I like the other features of Python.
Offline
Pages: 1