Re: Python class and variable issue(newby question!)

2013-03-29 Thread Dave Angel

On 03/29/2013 06:17 PM, Sam Berry wrote:

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.

--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python class and variable issue(newby question!)

2013-03-29 Thread Chris Angelico
On Sat, Mar 30, 2013 at 9:51 AM, Steven D'Aprano
 wrote:
> On Sat, 30 Mar 2013 08:24:00 +1100, Chris Angelico wrote:
>
>> On Sat, Mar 30, 2013 at 8:12 AM, Sam Berry 
>> wrote:
>>> class test()
>>> s = 1
>>>
>>> def test1()
>>> global s
>>> s = 2
>
>> That's not a global, that's a class variable.
>
>
> /me thwacks Chris with a halibut.

Ow ow ow, I give in... I am suitably and appropriately thwacked.

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

Yes, I should have said class ATTRIBUTE. Sorry all! (Can I blame the
whole 8AM and still up thing? No? Mea culpa, anyhow.)

> 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.

Agreed. Unfortunately it permeates a lot of programmer's vocabularies,
mine included.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class and variable issue(newby question!)

2013-03-29 Thread Steven D'Aprano
On Sat, 30 Mar 2013 08:24:00 +1100, Chris Angelico wrote:

> On Sat, Mar 30, 2013 at 8:12 AM, Sam Berry 
> wrote:
>> class test()
>> s = 1
>>
>> def test1()
>> global s
>> s = 2

> 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.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class and variable issue(newby question!)

2013-03-29 Thread Chris Angelico
On Sat, Mar 30, 2013 at 9:17 AM, Sam Berry  wrote:
> 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.

Awesome! Always happy to help out.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class and variable issue(newby question!)

2013-03-29 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class and variable issue(newby question!)

2013-03-29 Thread Benjamin Kaplan
On Fri, Mar 29, 2013 at 2:12 PM, Sam Berry  wrote:
> 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).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python class and variable issue(newby question!)

2013-03-29 Thread Chris Angelico
On Sat, Mar 30, 2013 at 8:12 AM, Sam Berry  wrote:
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Python class and variable issue(newby question!)

2013-03-29 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list