for Python 3.7
I have a class with a class attribute (class-wide variable):
class foo:
var="value goes here"
as well as an instance variable created in the class's init method:
def __init__(self, var=var):
self.var=var
Passing class variable to parameter
The class variable has the same name as init's parameter, and this doesn't confuse the interpreter, because the interpreter treats any field to the left of an "=" sign as a new variable within the scope of the method. It accomplishes this by populating a new namespace (a dictionary of variables) for the method's scope, implemented either as an array: e.g. parameters[1] = "var" or an associative array: parameters['var'] = pointer_to_value. Then the interpreter looks inside the method body and substitutes the generic reference for any references to "var" that occur on the right side of an "=" sign. Actually, this is a lie, but it's simpler to understand that than what it really does:
The interpreter identifies the matching regular expression
.*= *var(,{0,1}| *) *(;{0,1}|\n*)and then passes the corresponding pointer_to_value to the program's call stack). Because of that, the interpreter doesn't care what the parameters are named and is oblivious to the ambiguity of var=var. The fact that the ambiguity can be resolved is a side-effect of the structure of the language, rather than an intentional design decision. After all, ask yourself, when defining a method, why would you access a variable from inside of the method that you are defining? And why would you ever call an assignment operation to a variable in the parent scope from within a method definition? These are both illogical actions, and their namespace possibilities are mutually exclusive, so the interpreter never needs to address the ambiguity.
Conversely, the interpreter treats the right side of the "=" sign as an existing value and searches the class's namespace for variable definitions.
Storing parameter in Instance Variable
Within the method, the instance variable also has the same name as the class variable and the parameter, and this works inside the init method, because the instance variable is accessed via the self reference, e.g.
self.varname = varname
The problem
Method Definition
I need to access the instance variable from within the method definition of another method and I want to use the same name for this function's parameter:
def lookup(self, var=var):
print(var)
Will the expression var = var get the class attribute or the instance attribute? What does methodname(self) do with self, exactly? Is self a reference to an actual object, or does it only change the behavior of the interpreter from from static method to instance method? Does the interpreter automatically contextualize the right hand side of the "=" sign as an instance attribute of whatever object is typed into methodname(object)?
Method Body
Within the method body, if I make an assignment to var...
def lookup(self, var=var):
var = var
- Will it store it in the class variable, the instance variable, or a new variable with the same name?
- Will it get the class variable, instance variable, or the method's variable?
- How do I explicitly reference these variables?
I have read the documentation and several OOP tutorials, and a recent book, and this is still not clear to me.