[Tutor] attribute of built-in type

Kent Johnson kent37 at tds.net
Sat Nov 29 14:29:14 CET 2008


On Fri, Nov 28, 2008 at 6:01 PM, spir <denis.spir at free.fr> wrote:
> Kent Johnson a écrit :

>> I'm not sure what you are trying to accomplish. Custom classes have
>> __name__ attributes; instances of built-in and custom classes don't
>> have __name__ attributes.
>
> Hem, what do you mean? Functions, methods, classic and new style classes,
> also modules etc... all have a __name__. They are instances of built-in
> types (Er, for modules, I actually don't know -- yes, just checked it). As I
> understand it, only instances of custom types and of "value" types (such as
> int, str) do not have it.

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.

I guess what these all have in common is that there is special support
in the compiler for creating them. In the case of new-style clases,
this support is exposed in the metaclass mechanism - the class name is
passed to the metaclass constructor explicitly. For functions and
modules, the mechanism is not exposed AFAIK.

> 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'

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.

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...

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...

> (*) 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?

You can create classes whose instances are callable by defining a
__call__() method in the class.

Kent


More information about the Tutor mailing list