I want to access class variables inside the init
class Hare:
APP = 10;
def __init__(self):
print(APP)
h = Hare()
its giving error
!!! NameError: name 'APP' is not defined
You probably want :
class Hare:
APP = 10
def __init__(self):
print(self.APP) # 10
h = Hare()
Related
I am trying to call a class variable within a staticmethod, but when I called I am getting an error "hello" is not defined. any advise ?
class hello:
random1 = []
#staticmethod
def sub(x):
hello.random1.append(x -1)
sub.__func__(2)
if __name__ == "__main__":
print(hello.random1)
hello doesn't exist as a global name until you dedent out of the class definition (at which point the class is created and assigned to the global name hello). Change the code to:
class hello:
random1 = []
#staticmethod
def sub(x):
hello.random1.append(x -1)
hello.sub(2)
so sub is invoked after hello exists, and it will work.
I have a program like :
class ABC:
q = {}
def update:
self.state = (xx,xx)
global q
if self.state in q:
// do something
I am getting the error :
"NameError: global name 'q' is not defined"
Im new to python and need some help.
You can move q outside of the class:
q = {}
class ABC:
def update:
self.state = (xx,xx)
global q
if self.state in q:
# do something
pass
or you can reference q as a class variable:
class ABC:
q = {}
def update:
self.state = (xx,xx)
if self.state in ABC.q:
# do something
pass
q isn't being declared as a global variable here - it's being declared as a class variable of class ABC.
If you want q to be global, you should define it before you start declaring the class.
Hi I have staring programming with c and I conld'nt understand value scope in python.
here is my code
class ScenarioEnvironment():
def __init__(self):
print(self)
class report():
config = ScenarioEnvironment()
def __init__(self):
self.config = ScenarioEnvironment()
what happens passing config and config at init()?
and I wonder the value scope which config will be class valuable?
You need to know the differences between class attribute and instance object attribute.
Maybe these codes will help you:
class TestConfig1(object):
config = 1
def __init__(self):
self.config = 2
class TestConfig2(object):
config = 1
def __init__(self):
self.config2 = 2
if __name__ == "__main__":
print TestConfig1.config
t = TestConfig1()
print t.config
t2 = TestConfig2()
print t2.config
print t2.config2
more you can see the python blog.click here
Since your question seems a bit ambiguous, I'll just comment/fix your code:
class ScenarioEnvironment():
def __init__(self,x):
self.x = x # Assigning instance variable x to constructor parameter x.
print(self) # You're printing the object instance.
class report():
# Static variable shared amongst all classes.
config = ScenarioEnvironment(None) # Assigned to new instance of ScenarioEnvironment.
def __init__(self):
# No argument, must pass one (None).
# self.config is to a new ScenarioEnvironment instance.
self.config = ScenarioEnvironment(None)
Lets try out the classes.
Output:
s = ScenarioEnvironment(None)
r = report()
>>> <__main__.ScenarioEnvironment instance at 0x026F4238>
>>> <__main__.ScenarioEnvironment instance at 0x026F4300>
>>> <__main__.ScenarioEnvironment instance at 0x026F4350>
i have following issue with the below python code:
templates.py
class globalSettings:
def __init__(self):
self.tx_wait = 1200
self.tx_Interval = 30
general.py
from templates import *
class testSuit(object):
def __init__(self):
testSuit.settings = globalSettings()
def readMeasurements(self, filename, revision, lsv):
testSuit.settings.tx_wait = 100
testSuit.settings.tx_Interval = 25
test.py
import general
from templates import *
class testcase(object):
def __init__(self):
self.settings = general.testSuit.settings
But this gives me:
self.settings = general_main.testSuit.settings
AttributeError: type object 'testSuit' has no attribute 'settings'
The several imports that i do are needed for the rest of the code!
What i want to achieve is to be able to load different settings for the globalSettings class but have default values.
So, the def readMeasurements is actually reading new values if found from an excel sheet. This part is working fine!
What am i doing wrong in my coding?
Thanks for your time!
Assuming you want the variables to be instance specific:
class testSuit(object):
def __init__(self):
testSuit.settings = globalSettings()
def readMeasurements(self, filename, revision, lsv):
testSuit.settings.tx_wait = 100
testSuit.settings.tx_Interval = 25
Should be:
class testSuit(object):
def __init__(self):
self.settings = globalSettings()
def readMeasurements(self, filename, revision, lsv):
self.settings.tx_wait = 100
self.settings.tx_Interval = 25
Assuming you want the variables to be static instead of instance defined, you should be able to use the following:
class testSuit(object):
settings = globalSettings()
def readMeasurements(self, filename, revision, lsv):
settings.tx_wait = 100
settings.tx_Interval = 25
You can declare settings = globalSettings() outside of the init function (which isn't needed here).
With your current code you may be able to access the variables by using:
self.settings = general.testSuit.testSuit.settings
I have a little module that creates a window (program1). I've imported this into another python program of mine (program2).
How do I make program 2 get self.x and x that's in program1?
This is program1.
import Tkinter
class Class(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.Main()
def Main(self):
self.button= Tkinter.Button(self,text='hello')
self.button.pack()
self.x = 34
x = 62
def run():
app = Class(None)
app.mainloop()
if __name__ == "__main__":
run()
You can access the variable self.x as a member of an instance of Class:
c = Class(parent)
print(c.x)
You cannot access the local variable - it goes out of scope when the method call ends.
I'm not sure exactly what the purpose of 'self.x' and 'x' are but one thing to note in the 'Main' method of class Class
def Main(self):
self.button= Tkinter.Button(self,text='hello')
self.button.pack()
self.x = 34
x = 62
is that 'x' and 'self.x' are two different variables. The variable 'x' is a local variable for the method 'Main' and 'self.x' is an instance variable. Like Mark says you can access the instance variable 'self.x' as an attribute of an instance of Class, but the method variable 'x' is only accessible from within the 'Main' method. If you would like to have the ability to access the method variable 'x' then you could change the signature of the 'Main' method as follows.
def Main(self,x=62):
self.button= Tkinter.Button(self,text='hello')
self.button.pack()
self.x = 34
return x
This way you can set the value of the method variable 'x' when you call the 'Main' method from an instance of Class
>> c = Class()
>> c.Main(4)
4
or just keep the default
>> c.Main()
62
and as before like Mark said you will have access to the instance variable 'self.x'
>> c.x
34