Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global vs local variable
#6
(Jan-12-2018, 07:13 PM)Gribouillis Wrote:
(Jan-12-2018, 06:47 PM)mpd Wrote: Why? What benefit is gained by using classes here instead of methods?
There are two immediate benefits:

  1. Classes is the tool that generally avoids struggling with global variables management. Using instances creates a new space where names can be defined and shared between different functions.
  2. I guess @mquesnel's project is to add new features to his/her games and it's going to be much easier if one uses classes from the beginning.

If you look at the code, you notice that I achieve the OP's objective to play the second game if the first one succeeds, and I do this by using an instance member, and no global statement. You can do the same with a value returned, as you suggested, but it is less extensible.

The OP doesn't have a "global variables management" problem. He/she is simply trying to get the success/fail status back to the caller. If the OP doesn't know about or understand return statements, are classes an appropriate topic to bring up?

Regarding extensibility... you can have simple, extensible examples without using classes. And if the OP is doing something simple with kids, it's probably better to not use classes at first anyway.
Reply


Messages In This Thread
Global vs local variable - by mquesnel - Jan-12-2018, 03:01 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 03:19 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 04:15 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 06:47 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 07:13 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 07:33 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 08:31 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 10:59 PM
RE: Global vs local variable - by Larz60+ - Jan-12-2018, 10:45 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 11:49 PM
RE: Global vs local variable - by mpd - Jan-13-2018, 12:21 AM
RE: Global vs local variable - by mquesnel - Jan-13-2018, 06:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,255 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,884 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,083 Jan-27-2023, 05:32 PM
Last Post: caslor
  How to use global value or local value sabuzaki 4 1,211 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,966 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Global vs. Local Variables Davy_Jones_XIV 4 2,711 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Variable scope - "global x" didn't work... ptrivino 5 3,125 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Global - local variables Motorhomer14 11 4,365 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,271 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  from global space to local space Skaperen 4 2,384 Sep-08-2020, 04:59 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020