473,597 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class design (information hiding)

Hi all,

I'm wodering how the information hiding in python is ment. As I understand there
doesn't exist public / protected / private mechanism, but a '_' and '__'
naming convention.

As I figured out there is only public and private possible as speakin in "C++
manner". Are you all happy with it. What does "the zen of python" say to that
design? (protected is useless?)
class A:
def __init__(self):
self.__z = 1
self._z = 2
self.z = 3
def _getX(self):
return "X"
def __getY(self):
return "Y"
def doAnything(self ):
print self.__getY()
class B(A):
def __init__(self):
A.__init__(self )
print dir (self)
>>b = B()
['_A__getY', '_A__z', '__doc__', '__init__', '__module__', '_getX', '_z',
'doAnything', 'z']

I was a bit surprised about '_A__getY' and '_A__z'.

What would you say to a C++ Programmer about class interfaces in big Python
systems? What is the idea behind the _ and __ naming. Use or don't use '_'
methods ? (As Designer of the software, as Programmer of the software)

Regards Alexander


Sep 7 '07 #1
7 2023
Alexander Eisenhuth wrote:
As I figured out there is only public and private possible as speakin in
"C++ manner". Are you all happy with it. What does "the zen of python"
say to that design? (protected is useless?)
Ask it yourself:
>>import this
>
class A:
def __init__(self):
self.__z = 1
self._z = 2
self.z = 3
def _getX(self):
return "X"
def __getY(self):
return "Y"
def doAnything(self ):
print self.__getY()
class B(A):
def __init__(self):
A.__init__(self )
print dir (self)
>>b = B()
['_A__getY', '_A__z', '__doc__', '__init__', '__module__', '_getX',
'_z', 'doAnything', 'z']

I was a bit surprised about '_A__getY' and '_A__z'.
In what way exactly? "__*"-type methods are meant to be private to the
exact class they were defined in, so thats why you get these names
prefixed with "_A__". If you're confused why that happens at all, look
for "name mangling".
What would you say to a C++ Programmer about class interfaces in big
Python systems? What is the idea behind the _ and __ naming. Use or
don't use '_' methods ? (As Designer of the software, as Programmer of
the software)
Don't worry about information-hiding too much. If anyone is determined,
they can get at any information they want. You should just rely on
people not directly using a single-underscore method; thats how python
does it: trust instead of force.
Use the double underscore technique only when you *need* it, that is,
you don't want a method to be overridden by a subclass -- for whatever
reason that might be. Generally you can just forget about this type of
methods.

/W
Sep 7 '07 #2
Alexander Eisenhuth schrieb:
>
I'm wodering how the information hiding in python is ment. As I
understand there doesn't exist public / protected / private mechanism,
but a '_' and '__' naming convention.

As I figured out there is only public and private possible as speakin in
"C++ manner". Are you all happy with it. What does "the zen of python"
say to that design? (protected is useless?)
My favourite thread to this FAQ:

http://groups.google.at/group/comp.l...77ed1312e10b21

Greg
Sep 7 '07 #3
Bruno Desthuilliers schrieb:
Nope. It's either 'interface' (no leading underscore), 'implementation '
(single leading underscore), 'implementation with some protection
against accidental overriding' (two leading underscores).
What do you mean with 'implementation '? What does it express?
Sep 7 '07 #4
On Fri, 07 Sep 2007 15:17:06 +0200, Alexander Eisenhuth wrote:
Bruno Desthuilliers schrieb:
>Nope. It's either 'interface' (no leading underscore), 'implementation '
(single leading underscore), 'implementation with some protection
against accidental overriding' (two leading underscores).

What do you mean with 'implementation '? What does it express?
I guess he meant 'implementation detail', i.e. someone other then the
author of the class should not use until he really knows the
implementation and that this all might change without notice in the next
release.

Ciao,
Marc 'BlackJack' Rintsch
Sep 7 '07 #5
Alexander Eisenhuth a écrit :
Bruno Desthuilliers schrieb:
>Nope. It's either 'interface' (no leading underscore),
'implementatio n' (single leading underscore), 'implementation with
some protection against accidental overriding' (two leading underscores).

What do you mean with 'implementation '? What does it express?
The fact that a given attribute (or method - which are just callable
attributes FWIW) is an implementation detail, and not a part of the
class interface.
Sep 7 '07 #6
Gregor Horvath <gh@gregor-horvath.comwrot e:
Alexander Eisenhuth schrieb:

I'm wodering how the information hiding in python is ment. As I
understand there doesn't exist public / protected / private mechanism,
but a '_' and '__' naming convention.

As I figured out there is only public and private possible as speakin in
"C++ manner". Are you all happy with it. What does "the zen of python"
say to that design? (protected is useless?)

My favourite thread to this FAQ:
http://groups.google.at/group/comp.l...ad/thread/2c85
d6412d9e99a4/b977ed1312e10b2 1#b977ed1312e10 b21
Why, thanks for the pointer -- I'm particularly proud of having written
"""
The only really workable way to develop large software projects, just as
the only really workable way to run a large business, is a state of
controlled chaos.
"""
*before* I had read Brown and Eisenhardt's "Competing on the Edge:
Strategy as Structured Chaos" (at that time I had no real-world interest
in strategically managing a large business -- it was based on mere
intellectual curiosity and extrapolation that I wrote "controlled chaos"
where B & E have "structured chaos" so well and clearly explained;-).

BTW, if you want to read my entire post on that Austrian server, the
most direct URL is
<http://groups.google.a t/group/comp.lang.pytho n/msg/b977ed1312e10b2 1?>
....
Alex
Sep 9 '07 #7
Alex Martelli schrieb:
>
Why, thanks for the pointer -- I'm particularly proud of having written
"""
The only really workable way to develop large software projects, just as
the only really workable way to run a large business, is a state of
controlled chaos.
"""
Yes, indeed a good saying.
The problem is that we do not understand those complex systems, despite
the fact that some megalomaniacal people think they do.

Declaring a method as private, public or protected assumes that the
author fully understands the uses of this class by other programmers or
even himself later on.
But if the software is complex enough, chances are good that he does
*NOT* understand it fully at the time he writes "Protected" .

Programming means attempting. Attempt implies failure. Flexible systems
that are built for easy correction are therefore superior.

Greg
Sep 9 '07 #8

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

Similar topics

50
6331
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
18
1417
by: Jason Heyes | last post by:
Is this class evidence of poor design? class Rectangle { double width, height; public: double get_width() const { return width; } double get_height() const { return height; } void set_width(double width) { this->width = width; }
3
1541
by: DaveLessnau | last post by:
In a book on Data Structures that I'm reading, the authors are describing various linked lists and trees. In these, they start with some form of node class. What's driving me crazy is that they define these such that the data in those node classes is public. As a specific example (a node they're going to use to build linked lists): template <typename T> class node { public:
4
9771
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base class are the private components internal to the base class itself. What I want are ALL components for the entire class no matter how many levels of sub-classing this particular control contains. I do not want to have to force the child classes to...
8
2628
by: SpotNet | last post by:
Hello NewsGroup, I have a base class and six classes that inherit from this base class. All members in the base class are used in it's extended classes except, in one of the extended class one member in the base class is not required. Is it possible to mark a method in the extended class such that it does not appear in that particular extended class? I know you can use the 'new' keyword and redefine the method with a 'private'...
7
3452
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am trying to hide the `complexity` of template from the user interface. If you look at the code DataSet should be the object that my user manipulate. Unfortunately by doing so the object returned by DataSet::Get is a FloatingPt, so without the virtual...
6
2067
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple DeviceManager which is only interested in CD/DVD devices. I want to get the list of CD/DVD devices and "be informed when a disc inserted into a device". I am developing this on Linux. So, I used HAL API and read some system (actually /proc) files to gather...
14
1924
by: Jef Driesen | last post by:
I'm writing a library (to communicate with a number of devices over a serial port) and have some questions about the design. I have now a header and source file like this: /* device.h */ typedef struct device device; int device_open (device **dev, const char *name); int device_close (device *dev); int device_read (device *dev, void *data, unsigned int size);
27
2757
by: matt | last post by:
Hello group, I'm trying to become familiar with the information hiding design rules, and I have a lot (3) of questions for all you experts. AFAIK, a generic module has 2 files: ================ module.h ================ #ifndef __MODULE_HDR_INCLUDED__
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6698
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2408
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 we have to send another system
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.