Python class and variable issue(newby question!)

S

Sam Berry

Hey,

Im new to object orientated programming and have an issue with using classes. Im using the kivy module, a GUI creator , so posting the actual code may confuse. But an example of what im trying to achieve is below

class test()
s = 1

def test1()
global s
s = 2

def test2()
global s
s = 3

def test3()
global s
s = 4


class test4()

def printing()
print test().s

After been in the test()class a choice of buttons allows the user to run either of the functions within it and sends us into the test4() class. Then another button runs printing().

However printing() always prints 1, even if the variable has been changed. Im guessing because print test().s redefines the variable. May be a very simple to solve problem, but i cant figure it out.

Any insights of how to get this working, or example code would be very helpful!

Thanks, Sam
 
B

Benjamin Kaplan

Hey,

Im new to object orientated programming and have an issue with using classes. Im using the kivy module, a GUI creator , so posting the actual code may confuse. But an example of what im trying to achieve is below

class test()
s = 1

def test1()
global s
s = 2

def test2()
global s
s = 3

def test3()
global s
s = 4


class test4()

def printing()
print test().s

After been in the test()class a choice of buttons allows the user to run either of the functions within it and sends us into the test4() class. Then another button runs printing().

However printing() always prints 1, even if the variable has been changed. Im guessing because print test().s redefines the variable. May be a very simple to solve problem, but i cant figure it out.

Any insights of how to get this working, or example code would be very helpful!

Thanks, Sam
--


There are three different namespaces here: global, class, and local.
You're assigning to the global s, which exists outside the class. Do
"print s" instead of "print test().s" and you'll see your changed
value of s. If you want to change the value of s inside the class,
it's called "test.s" (notice that there's no parenthesis- that's
because s here is an attribute of the test class, not each instance of
test. The same value will be shared by all instances of that class).
 
C

Chris Angelico

class test()
s = 1

def test1()
global s
s = 2

def test2()
global s
s = 3

def test3()
global s
s = 4

That's not a global, that's a class variable. But to give any more
specific help, it'd be helpful to have some actual working code -
twiddle your code until it's a nice simple example:

http://sscce.org/

Most likely, what you want is to make these functions into either
static methods or instance methods. Beyond that, hard to say exactly.

ChrisA
 
S

Sam Berry

Thanks for the responses! My issue was sorted with Benjamins post, just printing s worked.

Cheers for the info though Chris, if i have any further issues il post them with some working code.

Sam
 
S

Steven D'Aprano

That's not a global, that's a class variable.


/me thwacks Chris with a halibut.


Not only is "class variable" ambiguous, but Python uses different scoping
rules for "variables" (name bindings) and attributes.

I don't know what language first decided to conflate object attributes
and variables -- I suspect Java -- but it is actively harmful terminology
(in Python at least, if not in general) and whoever invented it is bad
and should feel bad.
 
D

Dave Angel

Thanks for the responses! My issue was sorted with Benjamins post, just printing s worked.

Cheers for the info though Chris, if i have any further issues il post them with some working code.
In that case, you probably should add a line like:


s = None

at the beginning of the code, along with a description of what s is
supposed to represent (especially since the name doesn't reveal much).

And you should remove the class variable s. It'll just confuse things.

I guess this isn't the place to rail about non-const globals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top