-1
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 ;)

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • 2
    What are you trying to achieve with this, what is your usecase? **Why** does `type(SomeParam)` need to be the base class, instead of the `ParamMeta` metatype? What happens if there are *multiple* base classes? – Martijn Pieters May 28 '13 at 12:51
  • I want to be able the have simple and expressive assert statements e.g. assert type(SomeParam)==Param – jldupont May 28 '13 at 12:53
  • 2
    No, that is not a use case. That is not a reason to subvert the Python class and type system. Most of all, that is not a reason at all. – Martijn Pieters May 28 '13 at 12:54
  • Call it what you want, that's what I want. – jldupont May 28 '13 at 12:55
  • I am trying to help you achieve your goals here. What you want here may well be impossible. But if you give us the underlying reasons then we can perhaps help you find what *can* be done. – Martijn Pieters May 28 '13 at 12:56
  • The most common reason to want to do `type(Something) == OtherType` is instance type checking for subclasses, which is what `isinstance()` gives you, hence my answer. – Martijn Pieters May 28 '13 at 12:57
  • 2
    Specifically: Why do you want **type()** to do that, rather than a different function? –  May 28 '13 at 12:57
  • `isinstance` would be OK too. But it doesn't work as currently implemented. – jldupont May 28 '13 at 12:57
  • Then my question becomes: why **isinstance()**? That too does something completely different from what you want to do. Why do you want to shoehorn operation X into a function that does Y, rather than using a function that just does X? –  May 28 '13 at 12:58
  • That's why I wanted `type`. `type([])==list`. `SomeParam` is of type `Param` – jldupont May 28 '13 at 12:59
  • @jldupont `SomeParam` is of type `ParamMeta`, not `Param`. Inheriting from a class doesn't make it a type of that class. You should read this answer about metaclasses: http://stackoverflow.com/a/6581949/2032433 –  May 28 '13 at 13:00
  • 3
    No, `SomeParam` is *not* of type `Param`. You just confirmed my suspicion that this question is based on a big misunderstanding. Read my answer again. I attempted to address this misconception. –  May 28 '13 at 13:00
  • I understand all of this!!! I just want a solution to coerce this whole thing to work with the `type` function! – jldupont May 28 '13 at 13:01
  • 3
    @jldupont: There is no way to coerce the `type` function. We want you to tells us **what problem you are trying to solve**; there probably is no need to keep walking into this wall again and again. – Martijn Pieters May 28 '13 at 13:03
  • 1
    @jldupont: `issubclass(SomeParam, Param)` will return `True`. `isinstance(SomeParam(), Param)` returns `True`, but neither of those are what you want? – Martijn Pieters May 28 '13 at 13:04
  • 4
    @jldupont The reason we want to know what you're *really* trying to do is because this screams "[XY Problem](http://meta.stackexchange.com/q/66377)" at 120 dB. Whatever your problem is, I am confident that there is a good solution that does not involve what the text of your question is demanding. –  May 28 '13 at 13:05
  • Guys, 1M apologies. I got my head completely screwed up this morning. I understand now what you were trying to say: type(SomeParam)!=Param. How do I delete this abomination of a question? – jldupont May 28 '13 at 13:09

1 Answers1

6

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).

  • Exactly what my answer tried to address, but got downvoted for. :-) – Martijn Pieters May 28 '13 at 12:53
  • Please reread the question: I want a solution so that I have 'type(SomeParam)==Param'. I don't need the explanation as to why this doesn't work as described here. – jldupont May 28 '13 at 12:54
  • 2
    @jldupont That's like asking for `print(x)` to increment `x`. It is wrong and does not make sense. You can make define a function that gives you the/a superclass and you can test whether a something is a subclass of something else, but both operations are completely different from what `type()` does. –  May 28 '13 at 12:56
  • @jldupont Please reread the answer. He's trying to explain why what you're asking is not possible. – Bibhas Debnath May 28 '13 at 12:56