[Python-Dev] Object customization (was: Arbitrary attributes on funcs and methods)

Greg Stein gstein@lyra.org
Fri, 14 Apr 2000 14:56:37 -0700 (PDT)


On Fri, 14 Apr 2000, Vladimir Marangozov wrote:
>...
> Now, whenever there are two instances 'a' and 'b' of the class A,
> the first inconsistency is that we're allowed to assign attributes
> to these instances dynamically, which are not declared in the class A.
> 
> Strictly speaking, if I say:
> 
> >>> a.author = "Guido"
> 
> and if 'author' is not an attribute of 'a' after the instantiation
> of A (i.e. after a = A() completes), we should get a NameError.

I'll repeat what Gordon said: the current Python behavior is entirely
correct, entirely desirable, and should not (can not) change.

Your views on what an object model should be are not Python's views. If
the person who writes "a.author =" wants to do that, then let them. Python
does not put blocks in people's way, it simply presumes that people are
intelligent and won't do Bad Things.

There are enumerable times where I've done the following:

class _blank()
  pass

data = _blank()
data.item = foo
data.extra = bar
func(data)

It is a tremendously easy way to deal with arbitrary data on an attribute
basis, rather than (say) dictionary's key-based basis.

>... arguments about alternate classes and stuff ...

Sorry. That just isn't Python. Not in practice, nor in intent.

Applying metadata to the functions is an entirely valid, Pythonic idea.

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/