P
Paul Rubin
Bruno Desthuilliers said:Already possible - you just have to provide your own implementation of
__setattr__.
Part of the idea of non-dynamic attribute sets is to make the program
run faster, not slower.
Bruno Desthuilliers said:Already possible - you just have to provide your own implementation of
__setattr__.
Russ said:Yes, that would indeed be nice. I am certainly not the only one who
could use a language that is excellent for both research prototyping
*and* the final, safety-critical system. Then perhaps the prototype
could just be cleaned up and "hardened" for the end product rather
than rewritten in another language -- by programmers in another state
who may fail to understand many of the details that the prototype
developer agonized over.
2. There is also nothing inherent in a dynamic OO language that says
that class descriptors have to be mutable, any more than strings have
to be mutable (Python has immutable strings). I agree that being able
to modify class descriptors at runtime is sometimes very useful. The
feature shouldn't be eliminated from Python or else it wouldn't be
Python any more. But those occasions are rare enough that having to
enable the feature by saying (e.g.) "@dynamic" before the class
definition doesn't seem like a problem, both for encapsulation
and because it can also improve performance.
Russ P. wrote:
public = no leading underscore
private = one leading underscore
protected = two leading underscores
Should Python get strict and enforce access control
of object members ? No. Why ? I can think of several
reasons.
Give me one use-case where you strictly require
that members of an object be private and their
access enforced as such ?
Luis Zarrabeitia said:when you need to share some objects with potentially untrusted code (like, a
plugin system). You can't, and you shouldn't, expect that the user will know
what plugins he should or shouldn't load, and you shouldn't blame him/her
when your app stops working because you failed to protect it's internals from
malicious plugins (think web browser).
Russ:
1. Quit while you're ahead.
2. OOP is encapsulating data and functionality into a single grouping
(object).
3. Other features more recently developed by OO languages such as
Polymorphism, Access Control (a type of encapsulation), Inheritance
and Multiple Inheritance are all irrelevant and OO languages either
implement all or a subset of these features and each do so
differently.
Terry Reedy said:The question is where such checks should be. Guido prefers separate
checkers (PyChecker, PyLint) rather than in the interpreter.
Luis Zarrabeitia said:Why don't you do it backwards?
You *can* implement a metaclass that will remove the dynasmism from its
instances. Do it - I can give you a starting point if you wish.
But most of us are very happy with the dynamic nature of python... I chose
python _because_ of it.
Btw, for performance, there is __slots__,
with the side-effect that it forbids attribute creation 'on the
fly'.
Part of the idea of non-dynamic attribute sets is to make the program
run faster, not slower.
But, if something is done by convention, then departing from the
convention is by definition unconventional. If you do something
unconventional in a program, it could be on purpose for a reason, or
it could be by accident indicating a bug.
I don't understand why some folks spew such violent rhetoric against
the idea of augmenting Python with features to alert you automatically
when you depart from the convention, so that you can check that the
departure is actually what you wanted. A lot of the time, I find, the
departures are accidental and automated checks would save me
considerable debugging.
Bruno Desthuilliers said:Russ argument was about "good engineering", not about raw perfs. FWIW,
_you_ may be willing to trade dynamism for raw perfs, but there are
probably some people here that won't agree.
Bruno Desthuilliers said:Given that the convention for "protected" attributes in Python is to
prefix them with an underscore, I fail to see how one could
"accidentally" mess with implementation details. Typing a leading
underscore is rarely a typo.
Python is not set up for this even slightly. Java attempts it, with
mixed success.
That's kind of interesting, how does it work?
I like it too, since it is indispensable in some situations. But,
those situations are uncommon enough that I don't mind typing a few
extra keystrokes to turn the dynamism on.
That is a good point, we somehow lost sight of that in this thread.
I have had the impression that this is a somewhat accidental side
effect and shouldn't be relied on.
I think you are the one who is confused. Part of the problem here is
that the term "encapsulation" has at least two widely used meanings
(in the context of programming). In one sense, it just means grouping
data and methods together. In another sense, it means restricting the
client's access to data or methods.
Someone earlier on this thread
tried to claim that the first meaning applies to OOP, but Wikipedia
(and many other sources) say just the opposite.
People here are trying to claim that the leading underscore
conventions used with Python are essentially equivalent to
encapsulation. That is nonsense, of course.
But the point is that if you don't like
encapsulation,
then by definition you don't like OOP. You may like
certain features of OOP, but you don't like it in general. That's
about all their is to it.
And by the way, please don't bring up the canard that I am some kind
of OO zealot. I think OO is overrated, and I don't Java, in part
because it forces everything to be OO. The issue here is not my
personal opinion of OOP. This issue is one of widely accepted
definitions within the OO community.
Of course it's not a "requirement that programming languages must
implement." It's only a requirement if they want to be OO languages.
You're kidding, right? Think about a ten-million line program being
developed by 100 developers.
Bruno Desthuilliers said:You'll find successful "monster" projects written in
languages that are even more permissive than Python (C anyone ?),
Luis Zarrabeitia said:
Obviously there will never be total unanimity about every tiny thing.
I haven't anywhere in this thread as far as I know suggested
eliminating dynamism from Python,
which would be in "that's not Python
any more" territory. But, in the dozens of class definitions I write
in any given day of coding, I might use the dynamism we're talking
about in 1% of them at most.
If having to type a few extra keystrokes
on that 1% improves program reliabiity AND performance, it certainly
seems worth it to me.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.