Python and Variable Scope - python

So I am recently new to Python, but I seem to be able to program some stuff and get it working. However I've been trying to expand my knowledge of how things work in the language, and putting this simple file together confuses me.
class TestA:
def __init__(self):
self.varNum = 3
def printNum(self):
print(self.varNum)
class TestB:
varNum = 0
def __init__(self):
varNum = 3
def printNum(self):
global varNum
print(varNum)
a = TestA()
a.printNum()
b = TestB()
b.printNum()
The code to TestA prints 3 to the screen properly. However the code for TestB instead gives me a NameError stating that: 'varNum' is not defined. And I get that error whether i have the global varNum line there or not.
I suppose what confuses me, is I see the __init__ function as a class constructor. And when I have programmed with languages such as Java or C# I've declared global variables outside of the constructor so that their scope is the whole class. Is that not a thing in Python? The code I've written I just kind of tagged self. onto everything because I was just trying to get some stuff put together quickly, but I am trying to figure more out about the language now. Is self. the only way in Python to make class scope variables? Or are there other ways?
Thanks for your time :)

In Python, variables declared inside the class definition, but not inside a method are class or static variables:
class TestB:
varNum = 0
This creates a class-level varNum variable, but this is distinct from any instance-level varNum variable, so you could have:
class TestB:
varNum = 0
def __init__(self):
self.varNum = 3
b = TestB()
print(b.varNum) # print 3
print(TestB.varNum) # print 0
Thus, class TestB should work in this way:
class TestB:
varNum = 0
def __init__(self):
self.varNum = 3
def printInstanceNum(self):
print(self.varNum)
def printClassNum():
print(TestB.varNum)
b = TestB()
b.printInstanceNum() # print 3
TestB.printClassNum() # print 0
Note that since there's no any reference to instance object in method printClassNum(), we don't have to put self as an argument. The method could actually become a staticmethod:
class TestB:
varNum = 0
#staticmethod
def printClassNum():
print(TestB.varNum)
Ref
class objects
static method

Related

See function stop from locating defined class in Python

I'd like to define a class inside a function (for testing purpose) and put a value into
a function variable:
def foo():
myvar = None
class myclass:
def run(self):
myvar = 5
mm = myclass()
mm.run()
print(myvar)
The above prints None
Is there any way other than global to make the myvar variable accessible from the class? The correct answer would print 5
It's not possible to assign a value to a variable outside the current scope without global. If you need to persist the value within the class you can define class variables instead. Example:
def foo():
class Class:
var_to_change = None
def run (self):
self.var_to_change = 5
print (Class.var_to_change)
instance = Class()
instance.run()
print (Class.var_to_change)
I haven't tested the above code but it should work in theory.

Pass attribute to class method

How can I pass class attribute to a class method so that the attribute will be modified?
class foo:
def __init__(self):
self.diamond = 1
self.gold = 10
self.change(self.diamond)
self.change(self.gold)
def change(self, x):
x+=1
model = foo()
print(model.diamond)
output:
1
I want diamond becomes 2.
Is this a good solution for you?
class foo:
def __init__(self):
self.diamond = 1
def change(self):
self.diamond += 1
model = foo()
model.change()
print(model.diamond)
Let me say this first that you have no class method, or class variable in your example. What you have are instance variables and instance methods, note the self keyword. Now, with that said, you can access and modify your instance variables from any instance method, just like #Almog answered earlier.
The x in your change method is a local variable, basically it's not available outside your method. As for how you modify a variable by passing it to a function, it's not doable with your code I think. You would need something like a dataclass, which you can modify. Check out 'PassByValue' and 'PassByReference' concepts relating to this. Maybe someone else here can help with your particular situation.

Class Level Constructor for the Python Class

I understand that __init__() is the constructor for an instance of the class. It is called whenever a class is instantiated. There is also the concept of class variables -- variables that belong to the class and shared by all instances. For example:
class A(object):
a = 1
b = [] #a and b are shared by all instances of class A
But the problem is that sometimes it takes more code to initialize a and b than the one lines showing above. Therefore there is a need for 'class level constructor.' I wonder, though, if there is such a thing.
You can just do something like
class A(object):
# .......
var = input()
var += 10
# ecc.
A.b = var

How to reference a static attribute from within a class in Python?

I have the following python snippet:
class myClass:
myVar = 'a'
def __init__(self):
self.myOtherVar = 'b'
myVar = 'c' # Gets assigned but only as a local variable.
print myVar # prints 'a' !
print self.myOtherVar # says 'self' not found
My question is this;
What's the proper way to print the contents of myVar from within myClass and/or re-assign them from init?
The problem you are facing is because you don't understand how the scoping of class declarations work. A class declaration is executed in its own scope. After the execution is completed a new class object is created and the obtained scope is attached to the class as its __dict__.
Note: the class scope is not searched from within the methods scopes! This means that you have to reference class attributes as MyClass.attribute when inside a method definition.
For example:
class MyClass:
var = 1
# we are executing this code as a single block
# so you must reference the variable as is usual
print(var)
# default values are *not* inside the definition.
# they are evaluated in the outer scope, so use plain "var" here
def method(self, a_default=var):
print(a_default)
def other_method(self):
# inside methods you are in a different scope
print(MyClass.var)
# equivalent *if* no "var" instance attributes exists
print(self.var)
Note: since the class doesn't still exist when executing its declaration you cannot refer to MyClass at the "top level" of MyClass declaration:
class MyClass:
var = 1
print(MyClass.var) # error: MyClass still doesn't exist.
A side effect of this, is that the following code:
class MyClass:
x = 1
results = list(i+x for i in range(10))
Produces:
NameError Traceback (most recent call last)
<ipython-input-6-f1d4417b2e52> in <module>()
----> 1 class MyClass:
2 x = 1
3 results = list(i+x for i in range(10))
4
<ipython-input-6-f1d4417b2e52> in MyClass()
1 class MyClass:
2 x = 1
----> 3 results = list(i+x for i in range(10))
4
<ipython-input-6-f1d4417b2e52> in <genexpr>(.0)
1 class MyClass:
2 x = 1
----> 3 results = list(i+x for i in range(10))
4
NameError: name 'x' is not defined
Because generator expressions (and list-comprehensions in python3) are, in fact, considered functions with their own scope. Since the class scope isn't searched from inner function scopes the x cannot be found.
You can word around this using a function definition and default values:
class MyClass:
x = 1
def _make_results(x=x):
return list(i+x for i in range(10))
results = _make_results()
del _make_results # otherwise it would be added as a method.
# or:
results = (lambda x=x: list(i+x for i in range(10)))()
This isn't usually a problem since class definitions rarely contain anything other than method definitions and a few constants.
There are already a few questions on SO about class scopes:
The scope of names defined in class block doesn't extend to the methods' blocks. Why is that?
Short Description of the Scoping Rules?
Nested classes' scope?
Variable scopes in python classes
Why static binding works differently for class and function?
self.var will:
give var in self.__dict__ if present
give var in self.__class__.__dict__ if present
AttributeError
So use this or self.__class__.var if you want to access the static variable minding inheritance. If you extend myClass, the children instances will access the static variable in the child class.
If you want to access the static variable in myClass even when called from descendants, use myClass.var.
As for reassigning them, this must be done explicitly on the class object, or the assignment will just target the instance.
class myClass:
myVar = 'a'
def __init__(self):
self.myOtherVar = 'b'
print myVar # -> 'a'
class EmptyClass: pass
s = EmptyClass()
__init__(s)
myVar = s.myOtherVar
print myVar # -> 'b'
print myClass.myVar # -> 'b'

In python. class variable is static..is I want to use different instance. is there any solution?

In python, I want to make a class variable static so I can to use it from a different instance. is there any solution?
I don't follow your question exactly, but it seems to me you're asking how to make instance variables in Python. The answer is to set them inside a method, preferably __init__() using the self reference.
class Foo(object):
classVar = 0 #this is a class variable. It is shared between all instances
def __init__(self, instanceVar):
self.someVar = instanceVar
obj1 = Foo(10)
obj2 = Foo(42)
print obj1.classVar # prints 0
print obj2.classVar # prints 0
print obj1.someVar #prints 10
print obj2.someVar #prints 42

Categories

Resources