You are not logged in.

#1 2007-04-26 16:03:11

equadon
Member
Registered: 2007-04-12
Posts: 25

Python help

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

#2 2007-04-26 16:08:00

tom5760
Member
From: Philadelphia, PA, USA
Registered: 2006-02-05
Posts: 283
Website

Re: Python help

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.  smile

Good luck!

Offline

#3 2007-04-26 16:43:39

equadon
Member
Registered: 2007-04-12
Posts: 25

Re: Python help

tom5760 wrote:

Thanks. That was the page about private members I read actually.

tom5760 wrote:

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.  smile

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

#4 2007-04-26 17:22:20

tom5760
Member
From: Philadelphia, PA, USA
Registered: 2006-02-05
Posts: 283
Website

Re: Python help

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

#5 2007-04-26 18:29:31

equadon
Member
Registered: 2007-04-12
Posts: 25

Re: Python help

tom5760 wrote:

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

Board footer

Powered by FluxBB