Finding the name of a class

2007-01-08 Thread tim mosher
Hello I'm looking for a Larry Bates that was in the Navy.  Could this be
you??   In CT in 1965???  In your 60's??
Please let me know I have been searching for over 10 yrs  thanks
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Finding the name of a class

2006-08-03 Thread Shane Hathaway
John Salerno wrote:
 Shane Hathaway wrote:
 
 Don't forget to file a bug.
 
 I'm reluctant to call it a bug just yet. Here's more stuff below. 
 There's obviously a difference between old- and new-style classes. It 
 seems that as far as new-style is concerned, __name__ is an attribute of 
 __class__ (along with a bunch of other stuff), but not of Foo itself.

I'm not sure what you're saying.  The class of a class is the 'type' 
builtin, unless metaclasses are involved.  So your expression 
dir(Foo.__class__) is equivalent to dir(type), and the 'type' 
builtin happens to have a __name__ attribute that dir() notices.  Take a 
look:

  class Foo(object):
...   pass
...
  Foo.__class__ is type
True
  Foo.__name__
'Foo'
  Foo.__class__.__name__
'type'

The bug is that the expression dir(someclass), where the class is a 
user-defined class of either new or old style, never reveals to the user 
that the class object has a __name__ attribute.  I tested this with 
Python versions 2.3 through 2.5b1.  This is an education issue; since 
that important attribute is not in the list, newcomers are not likely to 
discover it, and may instead use strange incantations to get the name of 
a class.

Do you want me to file the bug?

Shane

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-03 Thread John Salerno
Shane Hathaway wrote:

 The bug is that the expression dir(someclass), where the class is a 
 user-defined class of either new or old style, never reveals to the user 
 that the class object has a __name__ attribute.

I guess maybe it is a bug. This seems to be the relevant code to prove it:

  class Foo(object):
pass

  dir(Foo)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
  Foo.__name__
'Foo'


Tim can report it if he wants, since he found it first. Otherwise I'll 
do it and it will give me a chance to see how the bug reporting process 
works.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-02 Thread Bruno Desthuilliers
Kirk Strauser wrote:
 Bruno Desthuilliers wrote:
 
 Kirk Strauser wrote:
 
 class foo(object):
 pass
 how can I find its name, such as:

 b = foo
 
 I suppose you mean b = foo() ?
 
 Actually, I meant 'b = foo' in this case - I want to find the name of the
 class that b references, 

Ok. Could have been a typo, just wanted to make sure.


 The name of a class is in the attribute '__name__' of the class. The
 class of an object is in the attribute '__class__' of the object.
 
 I swear that didn't work earlier.  Honest.  :-)

Not sure if it works for old-style classes...

 OK, now for the good stuff.  In the code below, how can I find the name of
 the class that 'bar' belongs to:
 
 class Foo(object):
 ... def bar(self):
 ... pass
 ...
 b = Foo.bar
 dir(b)
 ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', 
 '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', 
 '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 
 'im_self']

 b.im_class
class '__main__.Foo'
 b.im_class.__name__
'Foo'



 b.__class__

This will give you the class of b itself. Remember that in Python,
everything and it's sister is an object - including functions, methods,
classes and modules.

In this case, b is a method object - IOW a descriptor that wraps a
function object.


-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-02 Thread Bruno Desthuilliers
Dennis Lee Bieber wrote:
 On Tue, 01 Aug 2006 11:09:57 -0500, Kirk Strauser [EMAIL PROTECTED]
 declaimed the following in comp.lang.python:
 
 Actually, I meant 'b = foo' in this case - I want to find the name of the
 class that b references, but the name of an instance (which may have zero
 or many references to it).

   Pardon... That gives the class object another name, neither of which
 has any precedence over the other.

Not quite exactly. The 'b = foo' statement binds together name b and the
object foo - which in this case happens to be a class. OTOH, class
objects do have a __name__ attribute, which is not impacted by the binding.

 b = Foo.bar
 dir(b)
 ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', 
 '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', 
 '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'im_class', 
 'im_func', 'im_self']
 b.__class__
 type 'instancemethod'
 b.__class__.__name__
 'instancemethod'

 I was sort of hoping that it would print 'Foo'.
 
   But... you've just pulled the method out of the class
 definition... Without supplying an instance, there is no linkage to a
 class.

Yes there is. Method objects have an im_class attribute, that is a
reference to the class they were 'pulled out' from.


-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-02 Thread Bruno Desthuilliers
Larry Bates wrote:
 Kirk Strauser wrote:
 Given a class:

 class foo(object):
 pass
 how can I find its name, such as:

 b = foo
 print something(b)
 'foo'

 I'm writing a trace() decorator for the sake of practice, and am trying to
 print the name of the class that a traced method belongs to.  This seems
 like it should be easy, but I think I've been staring at the problem too
 long.
 
 print print b.__class__.__name__  gives what you want

Actually it should be b.__name__, since b refers to *class* foo.
b.__class__ is the metaclass (usually 'type' unless there's a custom
metaclass).



-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-02 Thread Kent Johnson
Kirk Strauser wrote:
 Larry Bates wrote:
 
 print print b.__class__.__name__  gives what you want
 
 That doesn't seem to do it, though.  Here's the result of importing a module
 from my company's internally-developed library:
 
 from Daycos.TableCopier.copyfro import StateProcessor
 print StateProcessor.__class__.__name__
 type
 
 I'm looking for something that would print 'StateProcessor' but am not
 having much luck.

It looks like StateProcessor is a class; StateProcessor.__class__ is the 
class of a class, i.e. type. Try
StateProcessor.__name__

Kent
-- 
http://mail.python.org/mailman/listinfo/python-list


Finding the name of a class

2006-08-01 Thread Kirk Strauser
Given a class:

 class foo(object):
 pass

how can I find its name, such as:

 b = foo
 print something(b)
'foo'

I'm writing a trace() decorator for the sake of practice, and am trying to
print the name of the class that a traced method belongs to.  This seems
like it should be easy, but I think I've been staring at the problem too
long.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Bruno Desthuilliers
Kirk Strauser wrote:
 Given a class:
 
 class foo(object):
 pass
 
 how can I find its name, such as:
 
 b = foo

I suppose you mean b = foo() ?

 print something(b)
 'foo'

The name of a class is in the attribute '__name__' of the class. The
class of an object is in the attribute '__class__' of the object.

 class Foo(object):
... pass
...
 b = Foo
 b.__name__
'Foo'
 b = Foo()
 b.__class__.__name__
'Foo'


 I'm writing a trace() decorator for the sake of practice, and am trying to
 print the name of the class that a traced method belongs to.  This seems
 like it should be easy, but I think I've been staring at the problem too
 long.

 help(dir)
Help on built-in function dir in module __builtin__:

dir(...)
dir([object]) - list of strings

Return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it:

No argument:  the names in the current scope.
Module object:  the module attributes.
Type or class object:  its attributes, and recursively the attributes of
its bases.
Otherwise:  its attributes, its class's attributes, and recursively the
attributes of its class's base classes.



-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Larry Bates
Kirk Strauser wrote:
 Given a class:
 
 class foo(object):
 pass
 
 how can I find its name, such as:
 
 b = foo
 print something(b)
 'foo'
 
 I'm writing a trace() decorator for the sake of practice, and am trying to
 print the name of the class that a traced method belongs to.  This seems
 like it should be easy, but I think I've been staring at the problem too
 long.

print print b.__class__.__name__  gives what you want

-Larry Bates
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Tim Chase
 class Foo(object):
 ... pass
 ...
 b = Foo
 b.__name__
 'Foo'

While this is surely true, would somebody explain why I had such 
trouble finding this?

  help(dir)
  Help on built-in function dir in module __builtin__:

continuing from your example...

  dir(b)
['__class__', '__delattr__', '__dict__', '__doc__', 
'__getattribute__', '__hash__', '__init__', '__module__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__str__', '__weakref__']
  '__name__' in dir(b)
False

'__name__' *really is* a method of b as shown by your example 
lines, and can be successfully called.  However, it *doesn't* 
show up when asked for via dir(b).  Grumble.

Is there a dir_and_i_really_mean_everything() function?

I suppose problems (mostly with expectations) can ensue when 
you've got dynamic attributes, but this seems like something that 
dir() should be finding.

-a puzzled tkc







-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread John Salerno
Tim Chase wrote:

 While this is surely true, would somebody explain why I had such trouble 
 finding this?

I think __name__ is an attribute of the class itself, not the instance:

  class Foo(object):
pass

  f = Foo()
  f.__name__

Traceback (most recent call last):
   File pyshell#4, line 1, in -toplevel-
 f.__name__
AttributeError: 'Foo' object has no attribute '__name__'
  Foo.__name__
'Foo'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Kirk Strauser
Larry Bates wrote:

 print print b.__class__.__name__  gives what you want

That doesn't seem to do it, though.  Here's the result of importing a module
from my company's internally-developed library:

 from Daycos.TableCopier.copyfro import StateProcessor
 print StateProcessor.__class__.__name__
type

I'm looking for something that would print 'StateProcessor' but am not
having much luck.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread John Salerno
John Salerno wrote:
 Tim Chase wrote:
 
 While this is surely true, would somebody explain why I had such 
 trouble finding this?
 
 I think __name__ is an attribute of the class itself, not the instance:

On the other hand:

  class Foo(object):
pass

  dir(Foo)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']



Hmmm.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Tim Chase
 While this is surely true, would somebody explain why I had such trouble 
 finding this?
 
 I think __name__ is an attribute of the class itself, not the instance:

That makes sense, but what doesn't make sense is why, when you do 
a dir(Foo), you don't get '__name__' in the returned list of 
available things Python knows about a Foo.

  class Foo(object):
... pass
...
  myClass = Foo
  myInstance = Foo()
  # does myClass have a '__name__' attribute?
  '__name__' in dir(myClass)
False
  # that's a negative, buster
  '__name__' in dir(myInstance)
False
  # haha, just kidding, it really did have a __name__
  # proof that dir() isn't showing everything:
  myClass.__name__
'Foo'
  myInstance.__name__
Traceback (most recent call last):
   File stdin, line 1, in ?
AttributeError: 'Foo' object has no attribute '__name__'


It's the

  '__name__' in dir(myClass)
False
  myClass.__name__
'Foo'

that throws me.  What other super-secret tricks have I missed 
because dir() didn't tell me about them?

-a still-confused tkc



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Shane Hathaway
John Salerno wrote:
   class Foo(object):
   pass
 
   dir(Foo)
 ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
 '__hash__', '__init__', '__module__', '__new__', '__reduce__', 
 '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
 
 Hmmm.

Don't forget to file a bug.

Shane
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Kirk Strauser
Bruno Desthuilliers wrote:

 Kirk Strauser wrote:

 class foo(object):
 pass
 
 how can I find its name, such as:
 
 b = foo

 I suppose you mean b = foo() ?

Actually, I meant 'b = foo' in this case - I want to find the name of the
class that b references, but the name of an instance (which may have zero
or many references to it).

 The name of a class is in the attribute '__name__' of the class. The
 class of an object is in the attribute '__class__' of the object.

I swear that didn't work earlier.  Honest.  :-)

OK, now for the good stuff.  In the code below, how can I find the name of
the class that 'bar' belongs to:

 class Foo(object):
... def bar(self):
... pass
...
 b = Foo.bar
 dir(b)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', 
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 
'im_self']
 b.__class__
type 'instancemethod'
 b.__class__.__name__
'instancemethod'

I was sort of hoping that it would print 'Foo'.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread John Salerno
Shane Hathaway wrote:

 Don't forget to file a bug.

I'm reluctant to call it a bug just yet. Here's more stuff below. 
There's obviously a difference between old- and new-style classes. It 
seems that as far as new-style is concerned, __name__ is an attribute of 
__class__ (along with a bunch of other stuff), but not of Foo itself.



  class Foo(object):
pass

  dir(Foo)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']

  dir(Foo.__class__)
['__base__', '__bases__', '__basicsize__', '__call__', '__class__', 
'__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__', 
'__flags__', '__getattribute__', '__hash__', '__init__', '__itemsize__', 
'__module__', '__mro__', '__name__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__subclasses__', 
'__weakrefoffset__', 'mro']

  class Foo:
pass

  dir(Foo)
['__doc__', '__module__']

  dir(Foo.__class__)
Traceback (most recent call last):
   File pyshell#9, line 1, in -toplevel-
 dir(Foo.__class__)
AttributeError: class Foo has no attribute '__class__'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Ziga Seilnacht
Kirk Strauser wrote:
[snip]
 OK, now for the good stuff.  In the code below, how can I find the name of
 the class that 'bar' belongs to:

  class Foo(object):
 ... def bar(self):
 ... pass
 ...
  b = Foo.bar

 print b.im_class.__name__
Foo


But if you are writing a decorator, you can use this code:

import sys

def tracer(func):

A decorator that prints the name of the class from which it was
called.

The name is determined at class creation time. This works
only in CPython, since it relies on the sys._getframe()
function. The assumption is that it can only be called
from a class statement. The name of the class is deduced
from the code object name.

classframe = sys._getframe(1)
print classframe.f_code.co_name
return func


Hope this helps,
Ziga

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Larry Bates
Dennis Lee Bieber wrote:
 On Tue, 01 Aug 2006 10:56:52 -0500, Kirk Strauser [EMAIL PROTECTED]
 declaimed the following in comp.lang.python:
 
 Larry Bates wrote:

 print print b.__class__.__name__  gives what you want
 That doesn't seem to do it, though.  Here's the result of importing a module
 from my company's internally-developed library:

 from Daycos.TableCopier.copyfro import StateProcessor
 print StateProcessor.__class__.__name__
 type

 I'm looking for something that would print 'StateProcessor' but am not
 having much luck.
 
   And what is StateProcessor
 
 class SP(object):
 ...   pass
 ... 
 print SP.__class__.__name__
 type
 
   Looks like it is, itself, the class, not something within the class.
 
 s=SP()
 print s.__class__.__name__
 SP
 DP = SP
 d = DP()
 print DP.__class__.__name__
 type
 print d.__class__.__name__
 SP

When I do this:

class foo(object):
pass

if __name__==__main__:
a=foo()
print a.__class__.__name__


Note: You must do it on instance of the class not the class itself.

it prints 'foo' for me.  Not exactly sure why you get something very
different.  I've used this LOTS of times in my code, but I'll admit
mostly with old-style classes.

-Larry Bates
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread bruno desthuilliers
Tim Chase a écrit :
 class Foo(object):

 ... pass
 ...

 b = Foo
 b.__name__

 'Foo'
 
 
 While this is surely true, would somebody explain why I had such trouble 
 finding this?

Mmm... Documentation needs update ?

   help(dir)
   Help on built-in function dir in module __builtin__:
 
 continuing from your example...
 
   dir(b)

 ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
 '__hash__', '__init__', '__module__', '__new__', '__reduce__', 
 '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
   '__name__' in dir(b)
 False
 
 '__name__' *really is* a method

s/method/attribute/

 of b as shown by your example lines, and 
 can be successfully called.  However, it *doesn't* show up when asked 
 for via dir(b).  Grumble.

Yes, as mentionned in the doc, dir() doesn't list *all* names in a 
namespace. DOn't ask me why nor how it chooses which ones it rejects, I 
wonder too.

 Is there a dir_and_i_really_mean_everything() function?

Perhaps in the inspect module...

-- 
http://mail.python.org/mailman/listinfo/python-list