473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Private/public module members

Hi, all!

In accordance with Python documentation, there are 2 ways to hide
data/methods inside the module (make them private):

1. have "public" members defined in __all__ list
2. start "private" members names with underscore.

Both methods for some reason "behave strange".

Here are 2 modules and the output:

#file: main.py ########################
import foo
print dir(foo)
foo.b()
foo._c()
foo.a()

#file: foo.py######################
import sys
__all__ = ["a"]
_private = 56
def b(): print 'b'
def _c(): print '_c'
def a(): print 'a'

Run main.py and here is the output:

['__all__', '__builtins__', '__doc__', '__file__', '__name__', '_c',
'_private', 'a', 'b', 'sys']
b
_c
a

Not only doc(foo) has '_c', '_private' and 'b', but one can call them
from outside the module.
It this "by design"?
Jul 18 '05 #1
2 1917
Absolutely! The "hiding" of private attributes/methods
doesn't make them completely unreachable. It just makes
them slightly "invisible". There is a third method of
"hiding" attributes/methods that is done by putting two
underscores (e.g. __attribute). This gets name mangled
to make it harder to call, but it can still be reached
if you know how. Unlike other languages Python always
allows you to get to the attributes/methods of a class,
even when they are "private". You should always reference
private attributes/methods with care.

Larry Bates
Syscon, Inc.

"Elbert Lev" <el*******@hotmail.com> wrote in message
news:94*************************@posting.google.co m...
Hi, all!

In accordance with Python documentation, there are 2 ways to hide
data/methods inside the module (make them private):

1. have "public" members defined in __all__ list
2. start "private" members names with underscore.

Both methods for some reason "behave strange".

Here are 2 modules and the output:

#file: main.py ########################
import foo
print dir(foo)
foo.b()
foo._c()
foo.a()

#file: foo.py######################
import sys
__all__ = ["a"]
_private = 56
def b(): print 'b'
def _c(): print '_c'
def a(): print 'a'

Run main.py and here is the output:

['__all__', '__builtins__', '__doc__', '__file__', '__name__', '_c',
'_private', 'a', 'b', 'sys']
b
_c
a

Not only doc(foo) has '_c', '_private' and 'b', but one can call them
from outside the module.
It this "by design"?

Jul 18 '05 #2
el*******@hotmail.com (Elbert Lev) wrote in
news:94*************************@posting.google.co m:
Not only doc(foo) has '_c', '_private' and 'b', but one can call them
from outside the module.
It this "by design"?


Yes.

All that prefixing with an underscore or listing names in '__all__' is
intended to do is to modify the behaviour of 'from foo import *'. It does
not prevent access, or even restrict visibility in any way.

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
5
by: zqhpnp | last post by:
class String { public: String& operator=(const String& str); private: char* pdata; } String& String::operator=(const String& str) { if(this==&str)
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
2
by: Bob | last post by:
Is this a bug? Expected behavior? Did I get the binding flags wrong? Bob -------------------- Imports System.Reflection Module Main
11
by: prefersgolfing | last post by:
I'm trying to find on MSDN, or someplace, that speaks to variables being public or private by default. Anyone know where? Thanks.
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.