Question

class ParamMeta(type):
  def __str__(self):
    return self.__name__


class Param(object):
  __metaclass__=ParamMeta

class SomeParam(Param):
  pass

What I want is for:

type(SomeParam)==Param

How do I achieve this?

Update: what do I need to change to have the desired behavior?

Update2: For posterity: this question is totally and utterly bogus. Please pretend you haven't seen it ;)

Was it helpful?

Solution

The type of the SomeParam class object is not Param, it's ParamMeta, and type() correctly reports that. You're confusing the is-a and inherits-from relationships. A SomeParam instance is-a Param. The SomeParam class object, on the other hand, inherits from the Param class object, but it is not an value of type Param.

For subclass relationships, there is the builtin issubclass(), which is the analogue of isinstance for is-a relationships: issubclass(SomeParam, Param) is true.

Edit: There is nothing you can do to achieve type(SomeParam) returning Param, except of course shadowing the name type with a hand-written function that returns the base class (which would be extremely misleading and bad style).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top