Code style - "hiding" functions inside other functions

I recently did things like this:

import Tkinter
class C(object):
    def __init__(self):
        self.root = Tkinter.Tk()
        def f():
            print 'hello'
        self.button = Tkinter.Button(master=self.root, command=f, text='say hello')

not something like this:

import Tkinter
class C(object):
    def __init__(self):
        self.root = Tkinter.Tk()
        self.button = Tkinter.Button(master=self.root, command=self.f, text='say hello')
    def f(self):
        print 'hello'

The question is not specific to Tkinter, but it is a good example. The function is fused only as a callback for the button, so I decided to define it inside __init__. Thus, only the code inside __init__even knows about the existence f- external areas do not begin to get clogged with names, and the user does not need to worry about loading methods intended for internal ones.

: ? , GUI- - __init__ , . , ?

+5
2

- Tkinter lambda.

self.button = Tkinter.Button(master=self.root, 
                             command=lambda:sys.stdout.write("Hello!\n"),
                             text='say hello')

, , , , - . , ( , ), ).

. - . __init__ , . ( _, from module import *) lambda , .

__init__ , . , , ( lambda), , , , ( gui).

, , - . , , , . (. ), , .

+8

. , :

1) (, pydoc )

2) (, ).

+2

All Articles