Find class of an instance?

2008-08-06 Thread Neal Becker
Sounds simple, but how, given an instance, do I find the class?

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


Re: Find class of an instance?

2008-08-06 Thread Heiko Wundram
Am Mittwoch, den 06.08.2008, 08:44 -0400 schrieb Neal Becker:
 Sounds simple, but how, given an instance, do I find the class?

inst.__class__

For example:

Python 2.5.2 (r252:60911, Aug  5 2008, 03:26:50)
[GCC 4.3.1] on linux2
Type help, copyright, credits or license for more information.
 x = hello
 x.__class__
type 'str'


--- Heiko.

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


Re: Find class of an instance?

2008-08-06 Thread Hugo

Neal Becker schreef:

Sounds simple, but how, given an instance, do I find the class?


I always do that with .__class__, not sure whether it is the best way:

 class A:
...  pass
...
 a = A()
 a.__class__
class __main__.A at 0xb7f01fbc
 a.__class__ == A
True
--
http://mail.python.org/mailman/listinfo/python-list


Re: Find class of an instance?

2008-08-06 Thread [EMAIL PROTECTED]
On 6 août, 15:52, Bruno Desthuilliers bruno.
[EMAIL PROTECTED] wrote:
 Heiko Wundram a écrit :

  Am Mittwoch, den 06.08.2008, 08:44 -0400 schrieb Neal Becker:
  Sounds simple, but how, given an instance, do I find the class?

  inst.__class__

 Works for new-style classes only. The generic way to go is to use
 type(obj).

 op
 Note that in both cases, what you'll get back is the class object, not
 the class name
 /op

My I should shut up and check my facts :( Please everybody ignore
this brain fart.


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


Re: Find class of an instance?

2008-08-06 Thread Nikolaus Rath
Neal Becker [EMAIL PROTECTED] writes:
 Sounds simple, but how, given an instance, do I find the class?


It does not only sound simple. When 'inst' is your instance, then

  inst.__class__

or

  type(inst) 

is the class.

Best,


   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
 -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list