2

I am learning from Learn Python the hard way where I have come across a study drill where they want to know that whether a class can be used like an object.

As I have experimented:

class A(object):
    def __init__(self):
        print "in here"
    def cat(self,name):
        print "my cat ",name

Here if I write A.cat(A(),"Tom"), it works and found out that its call unbound method. Now, does that mean I can assume that classes can be used like an object?

  • If memory serves, Python has a meta class which is the class for a class. So in that context, class is indeed an object; it is an instance of the metaclass. – Robert Harvey Jun 24 '14 at 15:53
2

Everything in Python is an object, including classes.

This means you can reference classes, passing them around like arguments, store them in attributes, (extra) names, lists, dictionaries, etc.

This is perfectly normal in Python:

class_map = {
    'foo': A,
    'bar': SomeOtherClass,
    'baz': YetAnother,
}

instance = class_map[some_variable]()

Now it depends on some_variable what class was picked to create an instance; the class_map dictionary values are all classes.

Classes are themselves instances of their type; this is called their metaclass. You can produce a new class by calling type() with a name, a sequence of base classes, and a mapping defining the attributes of the class:

type('DynamicClass', (), {'foo': 'bar'})

creates a new class object, with a foo attribute set to bar, for example. The class produced can itself then be used to create instances. So classes are produced by metaclasses, just as instances are produced by classes.

You can produce your own metaclasses by inheriting from type, opening a weird and wonderful world of class behaviour.

Calling an unbound method by passing in a separate instance is not really a good example of using classes as an object. All you did was use the initial reference (the class name) to look up the method, then pass in an instance of the class as the first parameter to stand in for self.

  • Thanks for those useful information.So,classes are instances of metaclasses and objects are instances of classes.So does that mean I can create instances of instances in python?That seems to be confusing! – Angel Jun 24 '14 at 16:06
  • No, instances are objects created from classes. Classes are objects too, but created from metaclasses. Functions are objects, methods are objects, files are objects, everything in Python is an object. – Martijn Pieters Jun 24 '14 at 16:07
  • What happens is that metaclasses and classes, like functions are callable objects. And when you call them they produce something; in Design Pattern language they are called factories. – Martijn Pieters Jun 24 '14 at 16:08
  • Okay,my first confusion is gone.Now if everything in python is an object,then are metaclasses objects too? – Angel Jun 24 '14 at 16:13
  • 1
    Metaclasses are objects too. Did I mention the term everything yet? – Martijn Pieters Jun 24 '14 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.