0

Let's take a look of the following example:

>>> class Foo(object):
...     pass
... 

My current understanding is when Python interpreter reads the line class Foo(object) [Foo class definition], it will create a Foo class object in memory.

Then I did the following two tests:

>>> dir()
['Foo', '__builtins__', '__doc__', '__name__', '__package__']

It looks like the Python interpreter has stored 'Foo' class object in memory.

>>> id(Foo)
140608157395232

It seems Foo class object is at memory address: 140608157395232.

Is my reasoning correct? If not, when does Python create class object in memory?

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
Mingyu
  • 31,751
  • 14
  • 55
  • 60

3 Answers3

5

To be more specific, Python creates the class type object when it finishes processing the entire class definition (so being pedantic, it wouldn't create it until it had parsed and processed the pass line as well, in your example).

This is typically only relevant in esoteric edge cases, like the following:

>>> class Foo(object):
...     print repr(Foo)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in Foo
NameError: name 'Foo' is not defined

But yes, your reasoning is generally correct.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Thanks, @Amber. The example you provided is just to the point! – Mingyu Aug 06 '13 at 05:26
  • Even more specific: after finding the end of the class definition, the class is created by invoking its metaclass, with the default metaclass being `type`. There is (much) more detail in http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python – torek Aug 06 '13 at 05:33
1

The class object is created by the line class Foo(object):, yes. It is not created when it reads that line, though, it's created when it reaches the end of the class definition.

The id of the class does not need to have any relation to the memory address. That's an implementation detail, and one you don't have any use of.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
0

A class object, rather than an instance is created at read or import time and any static class methods are created at that time.

A class instance is created and class.__init__() executed when you assign an object of that class type.

Just to complicate things another class may have a static member of your class so may create an instance at it's own declare time.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • I think `__init__` is to initialize variables, and `__new__` is to create class instance. – Mingyu Aug 06 '13 at 05:20
  • Also note that assignment has nothing to do with when `__init__` is called. `Foo()` on its own on a line still calls `Foo.__init__`. – Amber Aug 06 '13 at 05:22
  • @Mingyu is correct, the `__new__` function creates the instance (in Python2 that's part of why you write `class K(object)`, so as to inherit `object.__new__` if you don't define your own `__new__`; in 3.x the inheriting-from-`object` is implicit), and then its `__init__` function initializes it. Note that if your `__new__` returns a different object, that's what you get, e.g., `class K(object): def __new__(cls): return 1` (indent properly) and `k = K()` and you'll see that `k` is an ordinary `int` with value 1. – torek Aug 06 '13 at 05:44