Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 30th, 2003, 02:52 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
My class' privacy !

Damn, I was re-re-reading Eckel's "Thinking in C++" when I asked myself "Damn, are there any private and/or protected keywords in Python ?" I just hadn't done much OOP using Python. And no, private and protected aren't keywords in Python.

So, is there any way to create a class with protected and private members ?
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz

Reply With Quote
  #2  
Old December 30th, 2003, 04:33 PM
filerba filerba is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Oregon
Posts: 2 filerba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The short answer is sort of.
If you start a name with an underscore, it won't be imported when you say:
from foo import *
but it can still be accessed with:
import foo
foo._bar


A class member starting with a double underscore has its name (as seen outside the class) mangled to something like __ClassName__memberName.
I can't remember exactly, check the python docs.

So the long and the short of it is that you can tell people not to use your private members, but you can't stop them if they want to ignore you.

Reply With Quote
  #3  
Old December 31st, 2003, 10:14 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
filerba covered the finer points, but I just wanted to mention that the philosophy behind this is this: programmers know better what they want to do with the code that you provide than you do. It's a more "social" way of doing it whereas private/protected is a more guarded way of doing it. I vastly prefer Python's way of doing it because it simply makes more sense to me that I would know what I want to do better than someone else would. Granted, I may be misguided and may not be able to do what I want to with the given code, but that should be my problem and not the problem of the person providing that code. To me, in the end everybody wins with this scheme.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #4  
Old December 31st, 2003, 08:11 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
But, of course, it is possible to provide an environment where the end developer is severely inconvenienced through the use of metaclass programming. So, in theory, it is possible to create a private place within Python, if your absolutely feel a need to. In the end though, that need you may feel is often a product of an indoctrination where you have been taught that the only way to ensure proper usage of your libraries is to force a specific usage, rather that to encourage a specific usage, as is the "Python way".

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > My class' privacy !

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap