What is the difference between a 'Type' and an 'Object' in Python
object, python, types
Solution
Python's `super` function does different things depending on what it's arguments are. Here's a demonstration of different ways of using it:
class Base(object):
def __init__(self, val):
self.val = val
@classmethod
def make_obj(cls, val):
return cls(val+1)
class Derived(Base):
def __init__(self, val):
# In this super call, the second argument "self" is an object.
# The result acts like an object of the Base class.
super(Derived, self).__init__(val+2)
@classmethod
def make_obj(cls, val):
# In this super call, the second argument "cls" is a type.
# The result acts like the Base class itself.
return super(Derived, cls).make_obj(val)
Test output:
>>> b1 = Base(0)
>>> b1.val
0
>>> b2 = Base.make_obj(0)
>>> b2.val
1
>>> d1 = Derived(0)
>>> d1.val
2
>>> d2 = Derived.make_obj(0)
>>> d2.val
3
The `3` result is the combination of the previous modifiers: 1 (from `Base.make_obj`) plus 2 (from `Derived.__init__`).
Note that it is possible to call `super` with just one argument to get an "unbound" super object, it is apparently not useful for much. There's not really any reason to do this unless you want to mess around with Python internals and you really know what you're doing.
In Python 3, you can also call `super` with no arguments (which is equivalent to the calls in the functions above, but more magical).
Problem
I came upon this reading the python documentation on the `super` keyword: If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods). Can someone please give me an example of a distinction between passing a Type as a second argument versus passing an Object? Is the documentation talking about an instance of an object? Thank you.