Home > front end >  Easy way to OR 3 comparisons?
Easy way to OR 3 comparisons?

Time:06-28

I have an if block and I need to put a few comparisons in it for example:

if type(widget1) is tk.Entry or type(widget1) is tk.OptionMenu or type(widget1) is tk.Label:
    # do something

As you can see I am writing type(widget1) thrice. Instead, is there an easier way to do this?

if type(widget1) is tk.OptionMenu or tk.Entry or tk.Label:
    # do something

I know the above code wouldn't work here but just asking, is there an easier way to do it? I am making these kind of comparisons in multiple places, and so it would be helpful to write less. This isn't really a tkinter question, anyone can answer!! Thanks in advance!

CodePudding user response:

I think you can use a list :

if type(widget1) in [tk.OptionMenu, tk.Entry, tk.Label]:
    #Do something

CodePudding user response:

You can use isinstance which, in addition to its typical usage, accepts a tuple for its second argument:

isinstance(widget1, (tk.OptionMenu, tk.Entry, tk.Label))

See the docs.

  • Related