Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope in a python class surprising behaviour

Tags:

python

oop

The following code will result in the print statement being executed

class C1(object):

    print 'I am some code executing in C1'

    def method1(self):
        print 'I am method1'
    def method2(self):
        print 'I am method2'
        pass

I am surprised by this as I would have thought it would only execute if the class was instantiated, can anyone explain the thinking behind this?

like image 653
Mike Vella Avatar asked Mar 24 '26 01:03

Mike Vella


2 Answers

I guess you mean that it prints 'I am some code executing in C1'?

It's printed when the class object is created, and in this case that happens when you load the file it's declared in. This is also the place where you put class variables (which can be used as Python's equivalent of static variables in other languages).

If you want to have code that is executed upon creating instance, put it in __init__().

Official Python documentation on the subject: http://docs.python.org/tutorial/classes.html#class-objects

like image 189
vartec Avatar answered Mar 26 '26 18:03

vartec


In Python, a class definition is an executable statement.

To quote the documentation:

7.7. Class definitions

A class definition defines a class object (see section The standard type hierarchy):

classdef    ::=  "class" classname [inheritance] ":" suite
inheritance ::=  "(" [expression_list] ")"
classname   ::=  identifier

A class definition is an executable statement. It first evaluates the inheritance list, if present. Each item in the inheritance list should evaluate to a class object or class type which allows subclassing. The class’s suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [4] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace.

Programmer’s note: Variables defined in the class definition are class variables; they are shared by all instances. To create instance variables, they can be set in a method with self.name = value. Both class and instance variables are accessible through the notation “self.name“, and an instance variable hides a class variable with the same name when accessed in this way. Class variables can be used as defaults for instance variables, but using mutable values there can lead to unexpected results. For new-style classes, descriptors can be used to create instance variables with different implementation details.

Class definitions, like function definitions, may be wrapped by one or more decorator expressions. The evaluation rules for the decorator expressions are the same as for f> unctions. The result must be a class object, which is then bound to the class name.

like image 43
NPE Avatar answered Mar 26 '26 16:03

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!