I have a two part question.
>>> class One(object):
... pass
...
>>> class Two(object):
... pass
...
>>> def digest(constr):
... c = apply(constr)
... print c.__class__.__name__
... print constr.__class__.__name__
...
>>> digest(Two)
Two
type
How would one create object ‘Two’? Neither constr() or c() work; and it seems that apply turns it into a type.
What happens when you pass a class rather and an instance into a method?
The above comment was made in regards to this code:
apply(deprecated: see @pyfunc’s answer) certainly does not turn the classTwointo a type: It already is one.Classes are first class objects: they’re instances of
type. This makes sense if you look at the next example.You can see that a class very clearly functions as a type because it can be returned from
type. Here’s another example.You can see that
typeis a class that can be instantiated. Its constructor takes three arguments: the name of the class, a tuple of base classes and a dictionary containing the class attributes. It returns a newtypeaka class.As to your final question,
You’re going to have to be more specific. Classes are just instances of
typeand so are first class objects. Asking what happens if I pass a class into a method is like asking what happens if I pass an integer into a method: It depends entirely on what the method is expecting.