[Tutor] Properties of an object

Alan Gauld alan.gauld at btinternet.com
Fri Jan 30 21:50:33 CET 2009


"spir" <denis.spir at free.fr> wrote

> I was not thinking at data hiding or protection, rather at object 
> structure
> (or behaviour) specification. First, to the human -- and alse to the 
> machine.

Ah, OK, then in that case I am happier to agree that Python is unusual
(although not unique - Lisp can do the same) in its ability to add new
attributes - and even methods in V3 I believe - to an existing 
class/instance.
Indeed it's perfectly valid to do:

class Data: pass   ## no attributes or methods!

d1 = Data()
d2 = Data()

d1.foo = 'something'
d2.bar = 66

So that although d1 and d2 are both instances of Data their internal
attributes are very different both in name and content!.
BUT they both still adhere to the Data interface, they have merely
enhanced it. So:

dataset = [d1,d2]

is still perfectly valid as a homogenous collection.

Actually Lisp goes further than Python and allows us to switch
inherited classes around in the method resolution lookup etc
at run time, so

(foo myMethod)

might call different methods at different places in the same
program (don't do this kiddies, it will make your brain hurt!)
(I suspect this is possible in Python too using metaclasses,
but they make your brain hurt too so don't do it! :-)

> there is no real object specification; not even by class:

The specification is there but its a minimal specification
(lowest common denominator) rather than a comlete
specification for any given instance. Again its a difference
in perspective.

> In a rather consistent approach, Python does not provide
> any standard way to simply define/describe/structure an
> object -- or a class.

It does but not completely. You can be fairly sure that anything
defined in the class definition will be there, but there might be
other extras added is all. (although you can of course delete
attributes as well which does I admit kind of mess things up
- I don't like that feature although it is admittedly consistent
with the rest of the language)

> OO is well about defining objects that represent model
> components, aspects, agents, their behaviour, their relationships.

Yes, mostly although a broader view takes us back to the early
model of OOP as being about independent communicating
proceses. So in that model processes(objects) can acquire/lose
attributes during their lifetime without problem. The idea that
OOP was all about controllling interfaces was a later notion.
But I would agree that it is one that has become fairly well
established as a prime objectives of OOP.

> ... to remedy that, from doc/comment practices to attribute
> "introduction" in __init__, etc... All of these carry no real
> language semantics, instead for me they fulfill a human
> relevant semantic need -- that I was missing, and still miss.

I wouldn't argue with that. I've never found the ability to
add/delete attributes defined by the class to be useful,
although I'm sure somebiody can find a use case. But the
ability to dynamically augment an object is useful in a
language like Python. But I would like the assurance that
what was originally defined in the class was at least guaranteed!

My apologies for the misunderstanding.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list