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 11 9436
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.)
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.
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
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
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.
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
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.)
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/
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
>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/
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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() {}
|
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......
|
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....
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |