Python (3 and 2) doesn't allow you to reference a class inside its body (except in methods):
class A:
static_attribute = A()
This raises a NameError
in the second line because 'A' is not defined
, while this
class A:
def method(self):
return A('argument')
works fine. In other languages, for example Java, the former is no problem and it is advantageous in many situations, like implementing singletons.
Why isn't this possible in Python? What are the reasons for this decision?
EDIT: I edited my other question so it asks only for ways to "circumvent" this restriction, while this questions asks for its motivation / technical details.
Essentially, a class does not exist until its entire definition is compiled in its entirety. This is similar to end blocks that are explicitly written in other languages, and Python utilizes implicit end blocks which are determined by indentation.
The other answers are great at explaining why you can't reference the class by name within the class, but you can use class methods to access the class.
The
@classmethod
decorator annotes a method that will be passed the class type, instead of the usual class instance (self). This is similar to Java's static method (there's also a@staticmethod
decorator, which is a little different).For a singleton, you can access a class instance to store an object instance (Attributes defined at the class level are the fields defined as static in a Java class):
You can also use class methods to make java-style "static" methods:
Python is a dynamically typed language, and executes statements as you import the module. There is no compiled definition of a class object, the object is created by executing the
class
statement.Python essentially executes the class body like a function, taking the resulting local namespace to form the body. Thus the following code:
translates roughly to:
As a result, the name for the class is not assigned to until the
class
statement has completed executing. You can't use the name inside the class statement until that statement has completed, in the same way that you can't use a function until thedef
statement has completed defining it.This does mean you can dynamically create classes on the fly:
You can store those classes in a list:
Now you have a list of classes with no global names referring to them anywhere. Without a global name, I can't rely on such a name existing in a method either;
return Foo
won't work as there is noFoo
global for that to refer to.Next, Python supports a concept called a metaclass, which produces classes just like a class produces instances. The
type()
function above is the default metaclass, but you are free to supply your own for a class. A metaclass is free to produce whatever it likes really, even things that are bit classes! As such Python cannot, up front, know what kind of object aclass
statement will produce and can't make assumptions about what it'll end up binding the name used to. See What is a metaclass in Python?All this is not something you can do in a statically typed language like Java.
The answer is "just because".
It has nothing to do with the type system of Python, or it being dynamic. It has to do with the order in which a newly introduced type is initialized.
Some months ago I developed an object system for the language TXR, in which this works:
Here,
bar
is a static slot ("class variable") infoo
. It is initialized by an expression which constructs afoo
.Why that works can be understood from the function-based API for the instantiation of a new type, where the static class initialization is performed by a function which is passed in. The
defstruct
macro compiles a call tomake-struct-type
in which the(new foo)
expression ends up in the body of the anonymous function that is passed for the static-initfun argument. This function is called after the type is registered under thefoo
symbol already.We could easily patch the C implementation of
make_struct_type
so that this breaks. The last few lines of that function are:The
call_stinifun_chain
does the initialization which ends up evaluating(new foo)
and storing it in thebar
static slot, and thesethash
call is what registers the type under its name.If we simply reverse the order in which these functions are called, the language and type system will still be the same, and almost everything will work as before. Yet, the
(:static bar (new foo))
slot specifier will fail.I put the calls in that order because I wanted the language-controlled aspects of the type to be as complete as possible before exposing it to the user-definable initializations.
I can't think of any reason for
foo
not to be known at the time when that struct type is being initialized, let alone a good reason. It is legitimate for static construction to create an instance. For example, we could use it to create a "singleton".This looks like a bug in Python.
A class statement is executed just like any other statement. Your first example is (roughly) equivalent to
The first line obviously raises a
NameError
, becauseA
isn't yet bound to anything.In your second example,
A
isn't referenced untilmethod
is actually called, by which timeA
does refer to the class.