|
![](/i/fill.gif) |
Warp wrote:
> I surely can understand why such information could sometimes be useful,
> but it still sounds to me like you are accessing the internals of an object,
> bypassing its public interface, which is something that breaks the basic
> idea of modularity.
Here's an excellent example from Python:
http://qinsb.blogspot.com/2009/03/automatic-repr-and-eq-for-data.html
Translated for those who don't speak Python, this says "here's a mix-in
multi-inheritance class that provides a 'show as a string' function. It
examines the class and constructor arguments, so you can do something
like[1] this (using C++-ish syntax):
class Alpha : Beta, ThisMixinClass {
Alpha(int One, float Two) { ... }
}
And then call
Alpha alpha(23, 75.2);
and then you can say
string x = alpha.repr();
and have x get the value
"<class Alpha One=23 Two=75.2>"
Not really breaking encapsulation at all. Just adding as a method
functionality that you'd have to implement manually by hand in each class.
Not really breaking modularity any more than a template that relies on a
class having particular set of internal functions.
[1] The function __repr__() is what gets called on an instance to turn it
into a string that when you evaluate it in Python you get back the value. In
C++ terms, it would be like printing
std::string("Hello")
for a std::string whose value is "Hello", rather than just Hello.
__init__() is the constructor, and there's only one and you can't overload it.
--
Darren New, San Diego CA, USA (PST)
My fortune cookie said, "You will soon be
unable to read this, even at arm's length."
Post a reply to this message
|
![](/i/fill.gif) |