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?

2
  • 1
    I'm not sure what your question is. What do you mean by "when"? Commented Aug 6, 2013 at 5:12
  • I mean when does the Python interpreter create class object? What is the triggering event of class object creation? Commented Aug 6, 2013 at 5:15

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @Amber. The example you provided is just to the point!
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 stackoverflow.com/questions/100003/…
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.

3 Comments

Thanks, Lennart. The Python documentation says CPython's id does indicate memory address. docs.python.org/2/library/functions.html#id.
@Mingyu: Yeah, but that's CPython.
Agree... The id may or may not be the memory address depends on Python implementation :-)
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.

3 Comments

I think __init__ is to initialize variables, and __new__ is to create class instance.
Also note that assignment has nothing to do with when __init__ is called. Foo() on its own on a line still calls Foo.__init__.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.