Encapsulation unpythonic?

E

Ethan Furman

Ethan said:
Roy Smith wrote:

Nothing is accessible in Python except via getters and setters. The
only difference between Python and, say, C++ in this regard is that the
Python compiler writes them for you most of the time and doesn't make
you put ()'s at the end of the name

I think Roy is referring to the fact that attribute access is implemented
via __getattr__ / __getattribute__ /
__setattr__ / __delattr__. From one point of view, he's absolutely correct
- nearly all attributes are accessed via
getters/setters in Python.

Seems to me there is a difference between an underlying generic protocol for
data manipulation and "Python writing them
[getters/setters] for you".

Why? When I write "foo.bar", a bunch of generic code gets run which
figures out what value to return. If I don't like the generic behavior,
I can write my own __getattrr__(), etc, and make it do whatever I want.

I don't yet know C++ so I can't speak to it.

In Python if you are writing your own __getattr__, etc., you are writing the underlying protocol itself. To write a
getter/setter you would just use

def get_soemthing(...)
def set_something(...)

or, even better

@property
def something(...)

Maybe, as Steven said, it's just a matter of degree: my understanding of getters/setters is that they are per
attribute, while __getattr__ will be called for every attribute not otherwise found, __setattr__ will be called for
every assignment, __delattr__ will be called for every deletion, and all that is assuming you haven't written your own
__getattribute__ and completely changed the rules for your class.
 
8

88888 Dihedral

Fabrice Pombetæ–¼ 2013å¹´8月31日星期六UTC+8上åˆ1時43分28秒寫é“:
Fernando, it is widely accepted that Python pays very little attention toencapsulation as a principle set in stone. Chaz's definition of encapsulation is also mine. Now you need to consider that taking this principle off the hostel of OOP does not mean that you can do whatever you fancy and you can't make anything unsettable.



There are plenty of techniques within Python that allow you to protect your arguments (in particular, decorators) inside a Class.



Now, lets get to the pretentious philosophical discussion: I guess encapsulation is quite the opposite of, say, dynamic typing, which is arguably core in Python. In practice this allows Python to be less verbose: at the endof the day, if you look back at your previous languages, don't you find that some of their compulsory features are usually more of a pain than something useful in practice? And after all, whither encapsulation? Can't we justhave objects whose arguments are determined externally if we want to?

And that is the ballgame: as my old tutor says: "the claptrap of setters and getters does not need to be here if it is unnecessary". I would add: "so long as you can have them when you deem it necessary", and Python allows that.

The way to perform encapsulation in Python can be achieved by
writing methods in C to be compiled as an extension which
can be used by all instances of some classes in the upper level.

CYTHON is easy to be used for the job.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,102
Messages
2,570,646
Members
47,254
Latest member
GayMilline

Latest Threads

Top