Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42277 > unrolled thread

Python class and variable issue(newby question!)

Started bySam Berry <sambez_14@hotmail.co.uk>
First post2013-03-29 14:12 -0700
Last post2013-03-29 18:56 -0400
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Python class and variable issue(newby question!) Sam Berry <sambez_14@hotmail.co.uk> - 2013-03-29 14:12 -0700
    Re: Python class and variable issue(newby question!) Chris Angelico <rosuav@gmail.com> - 2013-03-30 08:24 +1100
      Re: Python class and variable issue(newby question!) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-29 22:51 +0000
        Re: Python class and variable issue(newby question!) Chris Angelico <rosuav@gmail.com> - 2013-03-30 09:59 +1100
    Re: Python class and variable issue(newby question!) Benjamin Kaplan <benjamin.kaplan@case.edu> - 2013-03-29 14:23 -0700
    Re: Python class and variable issue(newby question!) Sam Berry <sambez_14@hotmail.co.uk> - 2013-03-29 15:17 -0700
      Re: Python class and variable issue(newby question!) Chris Angelico <rosuav@gmail.com> - 2013-03-30 09:45 +1100
      Re: Python class and variable issue(newby question!) Dave Angel <davea@davea.name> - 2013-03-29 18:56 -0400

#42277 — Python class and variable issue(newby question!)

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-03-29 14:12 -0700
SubjectPython class and variable issue(newby question!)
Message-ID<78680654-5cf5-435c-9fce-19d6f5c23360@googlegroups.com>
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

[toc] | [next] | [standalone]


#42279

FromChris Angelico <rosuav@gmail.com>
Date2013-03-30 08:24 +1100
Message-ID<mailman.3965.1364592243.2939.python-list@python.org>
In reply to#42277
On Sat, Mar 30, 2013 at 8:12 AM, Sam Berry <sambez_14@hotmail.co.uk> 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

[toc] | [prev] | [next] | [standalone]


#42291

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-29 22:51 +0000
Message-ID<51561ae3$0$29974$c3e8da3$5496439d@news.astraweb.com>
In reply to#42279
On Sat, 30 Mar 2013 08:24:00 +1100, Chris Angelico wrote:

> On Sat, Mar 30, 2013 at 8:12 AM, Sam Berry <sambez_14@hotmail.co.uk>
> 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

[toc] | [prev] | [next] | [standalone]


#42293

FromChris Angelico <rosuav@gmail.com>
Date2013-03-30 09:59 +1100
Message-ID<mailman.3974.1364597970.2939.python-list@python.org>
In reply to#42291
On Sat, Mar 30, 2013 at 9:51 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Sat, 30 Mar 2013 08:24:00 +1100, Chris Angelico wrote:
>
>> On Sat, Mar 30, 2013 at 8:12 AM, Sam Berry <sambez_14@hotmail.co.uk>
>> 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

[toc] | [prev] | [next] | [standalone]


#42282

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2013-03-29 14:23 -0700
Message-ID<mailman.3967.1364592579.2939.python-list@python.org>
In reply to#42277
On Fri, Mar 29, 2013 at 2:12 PM, Sam Berry <sambez_14@hotmail.co.uk> 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).

[toc] | [prev] | [next] | [standalone]


#42288

FromSam Berry <sambez_14@hotmail.co.uk>
Date2013-03-29 15:17 -0700
Message-ID<6deb6120-e93d-429f-821f-437d25f0a761@googlegroups.com>
In reply to#42277
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

[toc] | [prev] | [next] | [standalone]


#42290

FromChris Angelico <rosuav@gmail.com>
Date2013-03-30 09:45 +1100
Message-ID<mailman.3972.1364597154.2939.python-list@python.org>
In reply to#42288
On Sat, Mar 30, 2013 at 9:17 AM, Sam Berry <sambez_14@hotmail.co.uk> 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

[toc] | [prev] | [next] | [standalone]


#42292

FromDave Angel <davea@davea.name>
Date2013-03-29 18:56 -0400
Message-ID<mailman.3973.1364597787.2939.python-list@python.org>
In reply to#42288
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web