Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make it easier not to create a lot of conditions if elif

def add_plot(axs, type_, x, y, y1=None, y2=None, height=None):
    try:
        if type_ in ['plot', 'scatter', 'stem', 'step']:
            _plot, = axs.plot(x,y)
            # but if type_ == 'scatter', I wouldn't want to write a separate condition for each type_
            # would like something like
            _plot, = setattr(axs, type_, {'x':x,'y':y})
    except Exception as e: 
        print(str(e))

Help, can anyone know the solution, thanks in advance

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use getattr

plot = getattr(axs, type_)(x, y)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading