Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:51:12+00:00 2026-06-11T10:51:12+00:00

Recently I’ve been doing things like this: import Tkinter class C(object): def __init__(self): self.root

  • 0

Recently I’ve been doing 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')

as opposed to 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 isn’t specific to Tkinter, but it’s a good example. The function f is only used as the callback for the button, so I’ve chosen to define it inside __init__. That way, only code inside __init__ even knows about f‘s existence – outer scopes don’t start getting cluttered up with names, and the user doesn’t need to care about a load of methods designed to be internal.

My question is: is this regarded as good style? I’m worrying because I’ve got a GUI class with quite a lot of buttons – __init__ is starting to look very long, with a lot of local function definitions. Is there a more appropriate alternative that I should be using?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:51:13+00:00Added an answer on June 11, 2026 at 10:51 am

    The typical way to do something like this in the context of Tkinter is to use a lambda function.

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

    At it’s core, this is really the same as your first example though, so if you prefer the first way — Go with it. I think that it is generally not idiomatic to create a new method in this case (unless you actually need the instance to be passed to the callback — in which case, you should do it the second way).

    There are two things to worry about here. The first is readability. If __init__ gets too cluttered to read, then you have a problem. You can move those functions to the module level (prefixed with an _ to keep them from importing in the from module import * context) and use lambda to bind local variables as arguments to those functions if necessary.

    If __init__ isn’t getting cluttered, then feel free to put the functions in there. Having the functions in there does mean that a new function is created every time a new instance is created (same with lambda) which I suppose could be wasteful as far as memory is concerned, but it shouldn’t be that big of a deal (especially in a gui).

    The second thing to worry about is namespace cluttering. However, if your module is so big that moving those local functions to module level functions creates a namespace issue, then your module is too big to begin with. As long as you prefix the functions with underscores (see suggestion above), you shouldn’t have namespace issues importing this module in other ones either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I've been doing quite the project mostly working with the DateTime class. Now,..
Recently I've been experimenting with the use of the Func<T> class, and so far
Recently I have been told that static class/methods are evil. Take for example my
Recently, I was writing a class in which I discovered that I could reduce
recently, while working on a db2 -> oracle migration project, we came across this
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently, we discovered odd behavior in some old code. This code has worked for
Recently I've read this performance guide Let's make the web faster and was puzzled
Recently I bought a new android tablet (a no-name Chinese tablet), and I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.