473,222 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

Class Friends


Hello!

Is it possible to make classes friends in Python? Or is it better to just stick
to "Java style" .isThis, .getThat, .setThis style functions?

I understand that classes are generally grouped logically into separate files
anyway. So, this isn't always necessary. But I'm a perfectionist and was
wondering if friendships were possible in Python.

Thank you!

se*******@yahoo.com
Jul 18 '05 #1
11 9367
On Fri, 27 Aug 2004 15:19:53 +0000, Sean Don wrote:

Hello!

Is it possible to make classes friends in Python? Or is it better to just stick
to "Java style" .isThis, .getThat, .setThis style functions?

I understand that classes are generally grouped logically into separate files
anyway. So, this isn't always necessary. But I'm a perfectionist and was
wondering if friendships were possible in Python.


In Python, there isn't much of an idea of "Private"; Python's philosophy
is "We're all consenting adults here." Thus, there isn't much of an idea
of "friends" either. In Java terminology, on a technical level everybody
is already friends with everybody else already.

Ultimately, it is the human meaning of "friendship" that is more
important (classes that twiddle with other classes internals), and Python
allows you to deal with that as you see fit.

In all my years of Python programming, only once have I generated a
"friend" class in the human sense. Generally there are ways around it.

As a final note, don't use 'getThat' or 'setThis'; that's a hack around
Java's weaknesses. Look up "property" in the Python manual and use that;
it is good both for re-factoring classes and for new ones too. This is one
reason that Python "gets away with" the way it works; unless you go out of
your way to break a class's encapsulation, the class author can always
catch things like attribute access with a property.

class MyXActsStrange(object):
def __init__(self):
self._x = 0
def getX(self):
return self._x + 1
def setX(self, val):
self._x = val
x = property(getX, setX)
a = MyXActsStrange()
a.x 1 a.x = 4
a.x

5
Very useful.

(Some people would use "self.__x", to get the psuedo-private name munging
invoked. My call is if the "security" isn't perfect there isn't much
point, as the language is already depending on the client programmer to
behave. I think Java can get good security but C++ is already effectively
as permissive as Python, it just makes the hoops harder.)
Jul 18 '05 #2
On Fri, 27 Aug 2004 16:55:28 GMT, Jeremy Bowers <je**@jerf.org> wrote:
(Some people would use "self.__x", to get the psuedo-private name munging
invoked. My call is if the "security" isn't perfect there isn't much
point, as the language is already depending on the client programmer to
behave. I think Java can get good security but C++ is already effectively
as permissive as Python, it just makes the hoops harder.)


Really, the double-under mangling is more about stopping a subclass
from stomping on some internal detail of the base class. In general,
though, I've found it to be a complete pain in the arse - there's been
too many times where I've been using an external library and wanted to
hook into the base class to fix something, and had to use the mangled
name.

I've considered a jihad to try and get uses of the double-under
mangling removed from the standard library. Maybe once 2.4 is out I'll
take this on.
Jul 18 '05 #3
Sean Don <a@c.d> wrote:
Hello!

Is it possible to make classes friends in Python? Or is it better to just stick to "Java style" .isThis, .getThat, .setThis style functions?
Neither. "friends" has no possible meaning in Python terms. Accessor
methods are a horrid idea in Python: if you ever need access to an
attribute to trigger the execution of some code, why, use 'property' --
that's what it's THERE for!

I understand that classes are generally grouped logically into separate files anyway. So, this isn't always necessary. But I'm a perfectionist and was
wondering if friendships were possible in Python.


What could 'friendship' possibly ever MEAN in a Python context...?!
Alex
Jul 18 '05 #4
Anthony Baxter <an***********@gmail.com> wrote:
...
I've considered a jihad to try and get uses of the double-under
mangling removed from the standard library. Maybe once 2.4 is out I'll
take this on.


Count on my support on this: I consider it "something that looked like
it would be a good idea at the time" (to me, too -- I'm not prescient
either;-) but didn't really work as well as hoped.
Alex
Jul 18 '05 #5
On Fri, 27 Aug 2004 16:55:28 +0000, Jeremy Bowers wrote:
In Python, there isn't much of an idea of "Private"; Python's philosophy
is "We're all consenting adults here." Thus, there isn't much of an idea
of "friends" either. In Java terminology, on a technical level
everybody is already friends with everybody else already.


My own 'rule of thumb' in accessing from outside to the attributes
of a Python class is "read attributes directly, but change them only via
methods". In other words, I treat all attributes in a class as 'readonly'.
This allow me to cut off amletic questions like "shall this be public of
private?", only to change it later on. The rule is so effective that I
tend to use it also in languages that allow public/protected/private
distinctions, like C++ and Java.
Jul 18 '05 #6
Sean Don wrote:
Hello!

Is it possible to make classes friends in Python?


Not just possible - all Python classes are always friends with each
other (something that rubs off Python programmers, I suppose :)

Shalabh

Jul 18 '05 #7
On Fri, 27 Aug 2004 20:37:44 +0200, Alex Martelli wrote:
Anthony Baxter <an***********@gmail.com> wrote:
...
I've considered a jihad to try and get uses of the double-under
mangling removed from the standard library. Maybe once 2.4 is out I'll
take this on.


Count on my support on this: I consider it "something that looked like
it would be a good idea at the time" (to me, too -- I'm not prescient
either;-) but didn't really work as well as hoped.


Well, I didn't want to come right out and say it for fear of starting a
flame war, but yeah, I was thinking this really loudly :-)

(At least it is only for *double* underscores; mangling on singles would
have *really* sucked.)
Jul 18 '05 #8
Shalabh Chaturvedi <sh*****@cafepy.com> pisze:
Is it possible to make classes friends in Python?


Not just possible - all Python classes are always friends with each
other (something that rubs off Python programmers, I suppose :)


That's why other call us "friendly people".

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #9
Jeremy Bowers <je**@jerf.org> wrote:
On Fri, 27 Aug 2004 20:37:44 +0200, Alex Martelli wrote:
Anthony Baxter <an***********@gmail.com> wrote:
...
I've considered a jihad to try and get uses of the double-under
mangling removed from the standard library. Maybe once 2.4 is out I'll
take this on.


Count on my support on this: I consider it "something that looked like
it would be a good idea at the time" (to me, too -- I'm not prescient
either;-) but didn't really work as well as hoped.


Well, I didn't want to come right out and say it for fear of starting a
flame war, but yeah, I was thinking this really loudly :-)

(At least it is only for *double* underscores; mangling on singles would
have *really* sucked.)


Yeah, it would. Mind you, accidental clashes with a base class's
internals _do_ happen, and it IS a pity there's no easy and elegant way
to avoid them, but it currently seems to me (after a few years'
experience, and starting with a positive bias towards the idea) that
mangling on double underscores, as Anthony says, has enough issues to
make it not ideal for the purpose. So, we need to keep that mechanism
in the language, for backwards compatibility (and perhaps as a sneaky
way to claim we DO so have 'private'...!-)... but we sure don't need to
use it ourselves if it gives us more problems than it solves.

Just like the (more practically important) problem of sandboxing (ever
since rexec and Bastion got deprecated), the problem of avoiding
accidental clashes between ancestor and descendant classes, without
paying a substantial price for it, may perhaps be currently classed as
'unsolved -- as yet'.
Alex
Jul 18 '05 #10
>Really, the double-under mangling is more about stopping a subclass
from stomping on some internal detail of the base class. In general,
though, I've found it to be a complete pain in the arse - there's been
too many times where I've been using an external library and wanted to
hook into the base class to fix something, and had to use the mangled
name.


It's more useful when using Mixin classes - as you say, a subclass could
validly want to mess with the implementation of a base class, but a mixin
shouldn't need to touch the internals of another.

--
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
Jul 18 '05 #11
Jeremy Bowers <je**@jerf.org> writes:
In Python, there isn't much of an idea of "Private";
**** ALERT: Rantbot trigger activation 8986232 ****

Rant title: Encapsulation and access restriction are orthogonal
concepts.

Rant variant: Python.

Rant summary:

The concept of privacy existis in Python. Please don't confuse
privacy with access restriction.

Python supports encapsulation. Please don't confuse encapsulation
with access restriction.

Encapsulation is data abtraction; access restriction is a pain in
the arse.

Rant body:

<SUPPRESSED>

Some people would use "self.__x", to get the psuedo-private name
munging invoked.


.... access restriction for the really dense.

If the programmer is too dumb to figure out the name mangling scheme
and work around it, then he (and the project he is working on) has
some serious problems, to the extent that accessing private data when
he shouldn't becomes but a drop in the ocean.

Jul 18 '05 #12

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

Similar topics

15
by: Guenther Sohler | last post by:
This is probably very easy to answer but for me its new. Yesterday I realized the need to be able to define a class within another one. So I have written some test code and its almost syntax error...
21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
5
by: Darren Grant | last post by:
Hi there, I've attempted to implement an Angle class. An Angle is a subset of an integer, where the range is [0,360). All other operations should be permitted. The code works, I think......
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
4
by: Andy Venikov | last post by:
Hi all, is there a way to make certain functions of a class be accessible only by specific class? Like "friend class whatever", but only for a few functions? This could be usefull in a...
4
by: BigMan | last post by:
This code does not compile on g++ 3.4.2. I want the template argument to be a friend of the template class. What syntax should I use to define such relationship? template< typename t > class t2...
21
by: anddos | last post by:
i am just wondering what is the friend in a class , ive got a book about c++ but it dosent explain deep enough . anyone care to make a example with a few notes on what the friend can benifit from....
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
14
by: Rahul | last post by:
Hi Everyone, I was searching for final class in c++, and i came across few links which suggests to have the constructor of the, to be final class, as private so that any derived class's...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.