#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    20
    Rep Power
    0

    Is there a __somename__ that gives class type ?


    For exemple if you do classname.__doc__ you will get the docstring of a class.

    Is there a similar way to get the class name of a class instance ?

    If there is would it be correct to iterate through a list of base class objects and check their real class ( since they all inherit baseclass ) using this value ?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can use the __class__ method to get the type of an instance, however we also have type() and isinstance() functions for doing this type of thing.

    Code:
    >>> a = object()
    >>> a.__class__
    <type 'object'>
    >>> b = dict()
    >>> b.__class__
    <type 'dict'>
    >>> c = str()
    >>> c.__class__
    <type 'str'>
    >>>
    >>> isinstance(c, object)
    True
    >>> isinstance(c, dict)
    False
    >>>
    I'm not really sure what you meant by iterating over the base classes. In Python every object inherits from the object class.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11
    hey try __module__ to get the class name.

    You can always see what all is avaiilable for a class by doing

    x=someclass()
    dir(x)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Code:
    class Foo(object): pass
    
    x = Foo()
    print str(x.__class__).split(".")[-1][:-2]
    Understanding why it works is left as an exercise... ;-)

    --OH.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    20
    Rep Power
    0
    Ok thanks.

    Sorry for being nebulous. I have a list of classes of a common base type but of different inherited types.

    I have a loop that iterates through all of the list objects and I wanted to identify which objects were of a certain type.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by bleakcabal
    Ok thanks.

    Sorry for being nebulous. I have a list of classes of a common base type but of different inherited types.

    I have a loop that iterates through all of the list objects and I wanted to identify which objects were of a certain type.
    The obvious answer is to use isinstance:
    Code:
    for obj in mylist:
        if isinstance(obj, Foo):
            obj.blah()
    The better answer, however, is generally to use Duck Typing:
    Code:
    for obj in mylist:
        try:
             obj.blah()
        except AttributeError:
             pass
    --OH.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    For the record:

    1) class instances have an attribute called __class__, that holds (surprise, surprise) the class object. type(obj) will also return the class of an object.

    2) class objects have an attribute __bases__, that holds a tuple of the classes they are derived from. However if you just want to test whether a class is derived from a particular base class then it is better to use the isinstance or issubclass functions.

    3) new-style classed (derived from object) have an undocumented method __subclasses__ that returns a tuple of the classes DERIVED from the class. This is used internally by Python, and since it is not documented may go away in future versions. It is still occasionally useful for things like debugging and exploring third-party libraries.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo