[Tutor] object's attributes
spir
denis.spir at free.fr
Fri Jan 2 20:05:16 CET 2009
On Fri, 2 Jan 2009 12:36:42 -0600
"Paul McGuire" <ptmcg at austin.rr.com> wrote:
> Denis -
Hello Paul,
pleased to read you again ;-)
> What you are seeing is standard procedure for any built-in type - no dynamic
> assignment of attributes allowed. Here is an analogous case to your
> example, but based on str instead of object:
>
> greeting = "bon jour"
> greeting.language = "French" # raises AttributeError: 'str' object has no
> attribute 'language'
>
> class I18NString(str):
> pass
>
> greeting = I18NString("bon jour")
> greeting.language = "French" # OK
Yep! I should have remembered that, as I have met this issue precisely with strings.
But this limitation is True only for the kind of built-in types that I personly call 'values':
numbers, strings etc... The following prints "I'm f!":
def f(): pass
f.view = "I'm %s!" % f.__name__
print f.view
[This is one more illustration that "values" are *not* standard objects, as I see it: both
conceptually (they represent qualities like qualifiers in human languages, while classes/types
represent.. classes, methods represent events and standard objects represents things) and
practically.]
> The example you cited of creating a thin derived class from object, for the
> simple purpose of supporting dynamically assigned attributes, sometimes goes
> by the name Bag, from the Smalltalk object framework. The Traits framework
> uses a "root" class HasTraits so that you can easily attach attributes, and
> then use the traits.ui package to bring up a GUI editor.
>
> Here is a recipe from the Python Cookbook:
> http://code.activestate.com/recipes/259174/
> There is also the new namedtuple type in the Python 2.6 collections module,
> which started out as this recipe:
> http://code.activestate.com/recipes/500261/.
I'll have a look at that.
> Happy New Year!
Wish you many sweet things.
I take the occasion to inform you I have a first version of parser
generator ready. [Still without text grammar translation.]
Someone (Eike) who works on parsers too pointed me to EasyExtend:
http://www.fiber-space.de/EasyExtend/doc/EE.html
Think you would find it interesting to allow for syntax freedom *inside* python. (but requires
preprocessing)
> -- Paul
Denis
------
la vida e estranya
More information about the Tutor
mailing list