Getting a class name

H

Harlin Seritt

Hi,

How does one get the name of a class from within the class code? I
tried something like this as a guess:

self.__name__

Obviously it didn't work.

Anyone know how to do that?

Thanks,

Harlin
 
D

deelan

Harlin said:
Hi,

How does one get the name of a class from within the class code? I
tried something like this as a guess:

self.__name__

Get the class first, then inspect its name:

HTH
 
G

goodwolf

I suppose that you wont get class name into its code (or before
definition end) but not into a method definition.


import sys

def getCodeName(deap=0):
return sys._getframe(deap+1).f_code.co_name


class MyClass (object):
name = getCodeName() + '!'
 
G

Gabriel Genellina

I suppose that you wont get class name into its code (or before
definition end) but not into a method definition.


import sys

def getCodeName(deap=0):
return sys._getframe(deap+1).f_code.co_name


class MyClass (object):
name = getCodeName() + '!'

What's the advantage over MyClass.__name__?
 
D

Daniel Klein

Why is the __name__ attribute not available to the instance? Why don't
normal lookup rules apply (meaning that a magic attribute will be
looked up on the class for instances) ?

Good question!
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']
Where is '__name__' ?


Dan
 
M

Michele Simionato

Why is the __name__ attribute not available to the instance? Why don't
normal lookup rules apply (meaning that a magic attribute will be
looked up on the class for instances) ?

Because __name__ isn't really an attribute, it is a descriptor defined
on
the metaclass:
type(type.__dict__['__name__'])
<type 'getset_descriptor'>

See http://users.rcn.com/python/download/Descriptor.htm for a guide to
descriptors, and the papers by me and David Mertz for a guide to
metaclasses.

Michele Simionato
 
F

Fuzzyman

Why is the __name__ attribute not available to the instance? Why don't
normal lookup rules apply (meaning that a magic attribute will be
looked up on the class for instances) ?

Because __name__ isn't really an attribute, it is a descriptor defined
on
the metaclass:
type(type.__dict__['__name__'])

<type 'getset_descriptor'>

Seehttp://users.rcn.com/python/download/Descriptor.htmfor a guide to
descriptors, and the papers by me and David Mertz for a guide to
metaclasses.

Thanks, I'll do some more reading.

I got as far as this on my own:

My guess is that it is because magic attributes are looked up on the
class. When you ask the *class* what its name is, the metaclass
answers on its behalf.

The class has no real ``__name__`` attribute, so when you ask the
instance (which doesn't inherit from the metaclass) you get an
``AttributeError``. Is this right ? In which case, how does the
metaclass (typically ``type`` which has many instance) know which
answer to supply ?

A simple test indicates that the name *is* looked up on the
metaclass :

... raw:: html

{+coloring} ... __name__ = 'fish'
... ... __metaclass__ = meta
... 'fish'
{-coloring}

So maybe ``__name__`` is a propery and ``type`` keeps a dictionary of
instances to names, registered in ``type.__new__`` ?

Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml
 
G

Gabriel Genellina

... name = C.__name__
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?

I were asking, why do you want a "name" attribute since "__name__" already
exists and has the needed information. And worst, using an internal
implementation function to do such task.
 
B

Bruno Desthuilliers

Gabriel Genellina a écrit :
I were asking, why do you want a "name" attribute since "__name__"
already exists and has the needed information. And worst, using an
internal implementation function to do such task.
This might be useful to avoid metaclass hacks when trying to initialize
a class attribute that would require the class name. (my 2 cents)
 
F

Fuzzyman

En Sun, 18 Feb 2007 20:56:48 -0300, Fuzzyman <[email protected]> escribió:





Having class decorators would be nice...

Yes - and Guido said he wouldn't be opposed to the idea when it was
poitned out that they would be very useful for both IronPython and
Jython to implement features of their underlying platforms.

I haven't heard anyone (other than you) mention them recently
though...

Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml
 
G

Gabriel Genellina

[somebody] wrote:
def getCodeName(deap=0):
return sys._getframe(deap+1).f_code.co_name
This might be useful to avoid metaclass hacks when trying to initialize
a class attribute that would require the class name. (my 2 cents)
It's still an ugly hack. :)
(But a nice implementation detail to know about none-the-less.)

Having class decorators would be nice...

Yes - and Guido said he wouldn't be opposed to the idea when it was
poitned out that they would be very useful for both IronPython and
Jython to implement features of their underlying platforms.

I haven't heard anyone (other than you) mention them recently
though...

Maybe it's a sign that actually nobody cares about class decorators :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,232
Messages
2,571,168
Members
47,803
Latest member
ShaunaSode

Latest Threads

Top