[Tutor] attribute of built-in type
Kent Johnson
kent37 at tds.net
Sun Nov 30 14:20:37 CET 2008
On Sat, Nov 29, 2008 at 5:09 PM, spir <denis.spir at free.fr> wrote:
> Kent Johnson a écrit :
>> On Fri, Nov 28, 2008 at 6:01 PM, spir <denis.spir at free.fr> wrote:
>>> Kent Johnson a écrit :
>> OK, functions (and methods, which are also functions, both of which
>> are instances of some builtin type), classes (which are instances of
>> type or classobj) and modules all have __name__ attributes.
>
> You're right, actually, as I was studying a bit how /some/ built-in types
> work, and they all had a __name__, I had a wrong impression about that
> attribute.
>
>>> Anyway, do you have an idea how to let custom objects inherit such
>>> attributes (*)? If they were of a custom type, one would just do it with
>>> inheritance (multiple, if needed). Now, this does not seem to work with
>>> buil-tins -- or I was unable to find the proper way.
>>
>> I don't understand this paragraph. When you say "custom objects" do
>> you mean classes or instances? Custom classes do have __name__
>> attributes; instances in general do not, other than the special cases
>> mentioned above.
>>
>> I *think* what you want is to be able to say something like
>> class Foo(object): pass
>> myFoo = Foo()
>> and then have
>> foo.__name__ == 'myFoo'
>
> Exactly, "custom objects" was a shortcut for "objects that are instances of
> custom types".
>
>> Is that right? If so, first recognize that the object can be assigned
>> to many names:
>> foo = bar = baz = Foo()
>> so what you are asking is not well defined.
>
> Right. I just need the first one. The object's birthname ;-) ; whatever it
> may be in case of "sequential naming", like in your example (which can
> happen in my case).
>
>> Second, search on comp.lang.python for some ideas, this request comes
>> up there from time to time.
>>
>> Third, how about passing the name to the constructor, or assigning the
>> attribute yourself?
>> myFoo = Foo('myFoo')
>> or
>> myFoo = Foo()
>> myFoo.__name__ = 'myFoo'
>> Yes, you have to type the name more than once...
>
> Actually, it is not an big issue. There are workarounds, anyway (such as
> exploring the current scope's dict /after/ object creation, and assigning
> its name back to an attribute -- this can also be automatised for numerous
> objects).
> Still, it would help & spare time & energy... This is also for me (and
> hopefully other readers) an opportunity to explore corners of the python
> universe ;-)
>
>> AFAIK Python does not have any hook to modify assignment which I think
>> is what you want. You might be able to do something like this with the
>> ast module in Python 2.6. You would have to load the code for a
>> module, compile it to the AST, modify the AST and compile it to code.
>> Some examples of this:
>> http://code.activestate.com/recipes/533145/
>> http://pyside.blogspot.com/2008/03/ast-compilation-from-python.html
>>
>> http://lucumr.pocoo.org/cogitations/2008/03/30/high-level-ast-module-for-python/
>>
>> I know you explained before but I still don't understand *why* you
>> want to do this...
>
> First, this problem occurs while I'm trying to use code of an external
> module for a project of mine. My case is probably specific, otherwise this
> issue would have been adressed long ago.
> These objects are kinds of "models" -- they control the generation of other
> objects -- but they are not classes, and can't be. As a consequence, the
> generated objects do not know what they actually are. They have a type, of
> course, but this type is general and not related to the real nature of the
> objects that share it. It would be a first step if the generators first
> would themselves know 'who' they are; then they would be able to pass this
> information to their respective "chidren". This would be a very small and
> harmful hack in the module's code (which is nice to study, not only for its
> organisation, also because it is not built upon further modules).
Do you know that you can probably just assign a __name__ attribute to
the objects? Or name, or whatever you like?
In [13]: class Foo(object): pass
....:
In [14]: f=Foo()
In [15]: f.name
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/kent/<ipython console> in <module>()
AttributeError: 'Foo' object has no attribute 'name'
In [16]: f.name = 'f'
In [17]: f.name
Out[17]: 'f'
This will work for custom objects that do not have a __slots__
attribute. Perhaps you could wrap the creation of the objects in a
function that gives them a name?
>>> (*) The point is that they can't be written by hand. How would you do it
>>> e.g. that an attribute automatically holds the objects own name? You can
>>> play with the namespace's dict, but it's the same thing. (It's like
>>> inheriting for instance an integer's behaviour: yes, you can rewrite it
>>> from
>>> scratch.)
>>> class myInt(int): works
>>> class myFunc(function): works not
>>> TypeError: Error when calling the metaclass bases
>>> type 'function' is not an acceptable base type
>>
>> I don't understand your point here. How would creating your own
>> function class help?
>
> It is an illustration of what i'm trying to do: let a class inherit from
> 'function' so that its instances get a __name__. This wouldn't be a harmful
> overload I guess, as these objects have a __call__ method anyway (the reason
> why I chose 'function').
I doubt this would do what you want. AFAICT the function name is
assigned by the compiler, not by the function constructor. (That is a
bit of oversimplification but close enough. I think the compiler
creates a code object, passing its name to the constructor; when a
function object is wrapped around the code object, it pulls its name
from the code object.)
>> Kent
PS Please use Reply All to reply to the list.
>
> Thank you for your attention and help,
> Denis
>
>
>
More information about the Tutor
mailing list