Python class and variable issue(newby question!)

Discussion in 'Python' started by Sam Berry, Mar 29, 2013.

  1. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Mar 29, 2013
    #1
    1. Advertising

  2. 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).
    Benjamin Kaplan, Mar 29, 2013
    #2
    1. Advertising

  3. 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
    Chris Angelico, Mar 29, 2013
    #3
  4. Sam Berry

    Sam Berry Guest

    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
    Sam Berry, Mar 29, 2013
    #4
  5. 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
    Steven D'Aprano, Mar 29, 2013
    #5
  6. Sam Berry

    Dave Angel Guest

    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
    Dave Angel, Mar 29, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Baker
    Replies:
    3
    Views:
    304
    Kevin Spencer
    Feb 8, 2005
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    644
    Iain Norman
    Feb 16, 2005
  3. Bruno Desthuilliers

    Python on the web - newby question

    Bruno Desthuilliers, Sep 3, 2008, in forum: Python
    Replies:
    2
    Views:
    246
    SimonPalmer
    Sep 3, 2008
  4. Lionel Thiry
    Replies:
    19
    Views:
    354
    Randy Kramer
    Mar 15, 2005
  5. Coyote
    Replies:
    4
    Views:
    301
    Coyote
    May 11, 2012
Loading...

Share This Page