Re: Problem resizing a window and button placement

Liste des GroupesRevenir à cl python 
Sujet : Re: Problem resizing a window and button placement
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 24. Feb 2024, 15:37:49
Autres entêtes
Organisation : Stefan Ram
Message-ID : <globals-20240224152703@ram.dialup.fu-berlin.de>
References : 1 2 3
Thomas Passin <list1@tompassin.net> writes:
Well, yes, in Python a variable created inside a function or method is
local to that function unless you declare it global.

  Or equivalent,

  main.py

def f():
    globals()[ 'x' ]= 2 # not a declaration!

f()

print( x )

  _sys.stderr

2

But if you think you need it to be a global variable you
should rethink your design.

  "Global" variables in Python are actually just "module variable".
  Module variables are not so bad. If you think of the module as a
  singleton object it's kinda like a class.

  But the usual recommendation is to use a class. I.e., transform

  main.py

def write_global():
    globals()[ 'x' ]= 2

def read_global():
    print( globals()[ 'x' ])

write_global()
read_global()

  _sys.stderr

2

  into

class Object_:
    def write_field( self ):
        self.x = 2
    def read_field( self ):
        print( self.x )

object_ = Object_()
object_.write_field()
object_.read_field()

  _sys.stderr

2

For one thing, before the next time you use your global
variable the window size may have changed again.

  The OP might be sleep walking into writing his own ad hoc,
  informally-specified, bug-ridden, slow implementation of
  half of a geometry manager like "pack" or "grid".

Date Sujet#  Auteur
24 Feb 24 o Re: Problem resizing a window and button placement1Stefan Ram

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal