0

Edit

In comments inheritance was suggested however this is already being done and I've added additional code snippet to show.


There are a few similar questions of initializing instance variables outside of __init__ where instance variables are initialized further down in the class within another def function (method). This question isn't a duplicate of those questions.

I have three classes all declaring the same self.xxxx instance variables after the def __init__:

class AskQuestion(simpledialog.Dialog):
    """ Prepends "\n" to text passed.
        Appends "\n\nAre you sure?\n" to text passed.
        Allows text to be highlighted and copied to clipboard with CTRL+C.
        Blocks other windows from getting focus
        MON_FONTSIZE is temporary font size until configuration file set up.
    """

    def __init__(self, parent, title=None, text=None, confirm='yes',
                 align='center', thread=None, icon='warning'):
        self.confirm = confirm      # Append "Are you sure?" line?
        self.align = align          # data (text lines) alignment
        self.thread = thread        # The thread run before button click
        self.loop_no = 1            # Loop counter (not used yet)
        self.data = text            # data (text lines) for text box
        self.text = None            # Textbox widget
        self.icon = icon            # Warning, Error, Info, Question icons
        try:
            self.font = (None, MON_FONTSIZE)
        except NameError:
            self.font = (None, 10)

        # Shared functions
        self.wait_window = wait_window_func
        #self.body = body(self, parent)
        #self.body = body

        simpledialog.Dialog.__init__(self, parent, title=title)

How can these lines of code be spun out into a global function which is called to initialize the variables? I'm searching for a technique similar to the bash . (source command) or the C #include command except variables won't be sourced from another file, simply a global function within the current file (module).

FYI I'm looking for consistency and code reduction for tkinter simpledialog class wrappers for ShowInfo, AskQuestion, AskString, etc.

12
  • 1
    Create a base class that your three classes inherit from? Commented May 24, 2021 at 5:38
  • If all three classes have same init , insted creadte one class and then inherit it Commented May 24, 2021 at 5:39
  • 1
    refer this link Commented May 24, 2021 at 5:40
  • @NikhilSingh Thanks for the link. However inheritance is already being used and I updated question to show code. Commented May 24, 2021 at 11:28
  • 1
    @NikhilSingh Please add an answer. I'd be happy to accept and upvote! Commented May 25, 2021 at 23:00

1 Answer 1

1

in a a case where your classes share init methods you can reduce code by inherting them, like this

class A(B):

where B is a parent class, also according to the question you can inherit more than one classes,like this

class A(B,C):

I am providing a generic answer to avoid the same discussion as in comments

Sign up to request clarification or add additional context in comments.

1 Comment

I should point class C must be before class A.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.