GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jul-2009, 16:57
mengu88 mengu88 is offline
New Member
 
Join Date: Jul 2008
Posts: 3
mengu88 is on a distinguished road

Protected access in python classes?


I want to access a class member from derived classes, but not outside of the class hierarchy.
I know private access, (adding 2 underscores(_) to the variable/function name).
How can declare a class member as protected in a python class ?
I use Python 2.5 and Python 3.1
  #2  
Old 01-Aug-2009, 18:28
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Protected access in python classes?


You can't. Python doesn't have private, public, or protected classes like Java or other languages. That's why the underscores are used. You can simulate protected classes with a single underscore and private with double, but it doesn't actually prevent direct calling like Java.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 02-Aug-2009, 02:58
mengu88 mengu88 is offline
New Member
 
Join Date: Jul 2008
Posts: 3
mengu88 is on a distinguished road

Re: Protected access in python classes?


Thank you, I understand something. But I don't mean private or protected classes,
I ask, how to declare a class member(variable or function/method) as protected ?
If I didn't understand wrong, it's possible to implement it with single underscore. True ?
  #4  
Old 07-Aug-2009, 22:17
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Protected access in python classes?


Yes, you can use underscores to simulate protected members. However, there is nothing within the Python language that enforces it, i.e. someone can still use the member "in a bad way" by calling the member directly w/ the underscore.

The underscore essentially hides the member from inadvertent use, but if the "true" name is known, it can still be used. There is no way to prevent the member from being misused like in Java or C++; it's more like the honor system.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "main" java.lang.NullPointerException valley1girl Java Forum 12 18-Jun-2009 12:32
Electronic health care center in Python docesam Python Forum 1 16-Apr-2009 03:09
Neutral Python book - the Quest for the Impossible? Kimmo Python Forum 1 04-Aug-2007 12:56
Looking for opinions crystalattice Miscellaneous Programming Forum 6 27-Sep-2006 21:02
Re: Beginning Python Tutorial (Part 2) crystalattice Python Forum 2 31-Jul-2005 12:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.