type classobj not defined?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wesley Brooks

    #1

    type classobj not defined?

    Dear Users,

    I'm in the process of adding assert statements to a large piece of
    code to aid with bug hunting and came across the following issue;

    Using python in a terminal window you can do the following:
    >type(False) == bool
    True

    I would like to check that an object is a class, here's an example:
    >class b:
    .....def __init__(self):
    .........self.c = 1
    .....def d(self):
    .........print self.c
    >type(b)
    <type 'classobj'>

    But the following fails:
    >type(b) == classobj
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'classobj' is not defined

    For the time being I'll use b.__name__ == b to ensure I'm getting the
    right class. Is there a reason why the other types such as bool are
    defined but classobj isn't?

    I'm running the following version of python:

    Python 2.4.3 (#1, Jun 13 2006, 11:46:08)
    [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2

    Cheers,

    Wesley Brooks
  • Peter Otten

    #2
    Re: type classobj not defined?

    Wesley Brooks wrote:
    Dear Users,
    >
    I'm in the process of adding assert statements to a large piece of
    code to aid with bug hunting and came across the following issue;
    >
    Using python in a terminal window you can do the following:
    >
    >>type(False) == bool
    True
    >
    I would like to check that an object is a class, here's an example:
    >
    >>class b:
    ....def __init__(self):
    ........self.c = 1
    ....def d(self):
    ........print self.c
    >
    >>type(b)
    <type 'classobj'>
    >
    But the following fails:
    >
    >>type(b) == classobj
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'classobj' is not defined
    >
    For the time being I'll use b.__name__ == b to ensure I'm getting the
    right class. Is there a reason why the other types such as bool are
    defined but classobj isn't?
    No idea. You can easily define it yourself, though:
    >>class Classic: pass
    ....
    >>classobj = type(Classic)
    >>isinstance(Cl assic, classobj)
    True

    Note that "newstyle" classes (those deriving from object) are of type
    'type', not 'classobj':
    >>class Newstyle(object ): pass
    ....
    >>isinstance(Ne wstyle, classobj)
    False
    >>isinstance(Ne wstyle, (classobj, type))
    True

    The inspect module wraps the functionality in a function:
    >>import inspect
    >>inspect.iscla ss(Newstyle)
    True
    >>inspect.iscla ss(Classic)
    True

    Peter

    Comment

    • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

      #3
      Re: type classobj not defined?

      Wesley Brooks schrieb:
      >type(b) == classobj
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      NameError: name 'classobj' is not defined
      >
      For the time being I'll use b.__name__ == b to ensure I'm getting the
      right class. Is there a reason why the other types such as bool are
      defined but classobj isn't?
      In general, only those types which you also use as constructors are
      builtin. I.e. you would write bool(foo) (where foo is a variable),
      but you would not write classobj("bar") (to create a class named
      "bar"). That you can also use it for a type test is a side-effect.

      Some more types are available in the types module. In this case,
      you can use types. In this case, types.ClassType would do the trick.

      Regards,
      Martin

      Comment

      • Denis Kasak

        #4
        Re: type classobj not defined?

        Wesley Brooks wrote:
        Dear Users,
        >
        I'm in the process of adding assert statements to a large piece of
        code to aid with bug hunting and came across the following issue;
        >
        Using python in a terminal window you can do the following:
        >
        >type(False) == bool
        True
        >
        I would like to check that an object is a class, here's an example:
        >
        >class b:
        ....def __init__(self):
        ........self.c = 1
        ....def d(self):
        ........print self.c
        >
        >type(b)
        <type 'classobj'>
        >
        But the following fails:
        >
        >type(b) == classobj
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        NameError: name 'classobj' is not defined
        You can compare it against the ClassType object located in the types module.
        import types
        class b:
        .....def __init__(self):
        .........self.c = 1
        .....def d(self):
        .........print self.c
        type(b) == types.ClassType
        True

        --
        Denis Kasak

        Comment

        Working...