accessing meta information in Python 
Author Message
 accessing meta information in Python

Hello,

I'd like to access to the run-time information describing python class
hierarchies; especially for comparing types:
    Is type A a subclass of type B ?

I've searched in vain the feature in Python 1.5 (I was hoping to finc it
in the 'types' module)...

Thanks,

Frdric Giacometti
Arakn
Chateau de Beaufort
19320 La Roche Canillac, France
tel +33 555 29 25 00



Tue, 13 Mar 2001 03:00:00 GMT  
 accessing meta information in Python

Quote:
> I'd like to access to the run-time information describing Python class
> hierarchies; especially for comparing types:
>     Is type A a subclass of type B ?

> I've searched in vain the feature in Python 1.5 (I was hoping to finc it
> in the 'types' module)...

Use the 'issubclass()' built-in function,e.g.:

        >>> class B:
                def meth(self): print "B.meth()"

        >>> class C(B):
                def meth2(self): print "C.meth2()"

        >>> issubclass(B, C)
        0
        >>> issubclass(C, B)
        1

Also note 'isinstance()', e.g.:

        >>> c = C()
        >>> isinstance(c, C)
        1
        >>> isinstance(c, B)
        1
        >>> b = B()
        >>> isinstance(b, C)

--Guido van Rossum (home page: http://www.python.org/~guido/)



Tue, 13 Mar 2001 03:00:00 GMT  
 accessing meta information in Python

* Frederic Giacometti
|
| I'd like to access to the run-time information describing Python class
| hierarchies; especially for comparing types:
|     Is type A a subclass of type B ?
|
| I've searched in vain the feature in Python 1.5 (I was hoping to
| finc it in the 'types' module)...

If you want to know which classes are the base classes of A that
information is in the special variable A.__bases__. (It's a tuple.)

The place to look for this kind of information is in the language
reference.

If you want to check whether a is an instance of the B class (whether
directly or of a subclass of B) you can use the built-in function
isinstance like so:

  isinstance(a,B)

--Lars M.



Tue, 13 Mar 2001 03:00:00 GMT  
 accessing meta information in Python
Frederic asks:

Quote:
> I'd like to access to the run-time information describing Python
> class hierarchies; especially for comparing types:
>     Is type A a subclass of type B ?

Guido pointed you to issubclass and isinstance. To go deeper, look at
in the Library reference under Built-in Types.

- Gordon



Tue, 13 Mar 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. BlackBox Meta: accessing methods through Meta

2. Meta Line information in Python?

3. meta-circular-meta-interpreters (long)

4. #\Meta #\Control #\Meta-Control etc.

5. No meta-type in Python?

6. Q: Python & Windows Enhanced Meta File (EMF)

7. Python meta object question

8. python code for meta-search engine

9. Defining (meta-)models with Python

10. Meta object protocol for Python

11. Accessing mdb(access 2000) files on linux using python

 

 
Powered by phpBB® Forum Software