![]() |
|
#1
|
|||
|
|||
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
|
||||
|
||||
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
|
|||
|
|||
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
|
||||
|
||||
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 GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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