0

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.

Community
  • 1
  • 1
0x539
  • 1,489
  • 1
  • 13
  • 30

4 Answers4

4

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:

class Foo(object):
    bar = baz

translates roughly to:

def _Foo_body():
    bar = baz
    return locals()
Foo = type('Foo', (object,), _Foo_body())

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 the def statement has completed defining it.

This does mean you can dynamically create classes on the fly:

def class_with_base(base_class):
    class Foo(base_class):
        pass
    return Foo

You can store those classes in a list:

classes = [class_with_base(base) for base in list_of_bases]

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 no Foo 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 a class 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.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

A class statement is executed just like any other statement. Your first example is (roughly) equivalent to

a = A()
A = type('A', (), {'static_attribute': a})

The first line obviously raises a NameError, because A isn't yet bound to anything.

In your second example, A isn't referenced until method is actually called, by which time A does refer to the class.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

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.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

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):

class A(object):
    instance = None

    @classmethod
    def get_singleton(cls):
        if cls.instance is None:
            print "Creating new instance"
            cls.instance = cls()

        return cls.instance


>>> a1 = A.get_singleton()
Creating new instance

>>> a2 = A.get_singleton()

>>> print a1 is a2
True

You can also use class methods to make java-style "static" methods:

class Name(object):

    def __init__(self, name):
        self.name = name

    @classmethod
    def make_as_victoria(cls):
        return cls("Victoria")

    @classmethod
    def make_as_stephen(cls):
        return cls("Stephen")


>>> victoria = Name.make_as_victoria()
>>> stephen = Name.make_as_stephen()

>>> print victoria.name
Victoria
>>> print stephen.name 
Stephen
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100