r/learnpython • if MacItaly: return helpful / else: return to_Google • Nov 24 '20

Beginner/Intermediate Python Programmer Finally Using Multiple Files for Project

Holy Moley! I started playing around with building a mini-project using multiple files instead of one long file. Wait, let me back up...

I just started Python about a year ago, self-taught, and have been actively building desktop apps for my company in the CMP Semiconductor field. They're efficient applications that my boss loves and have been able to make us some good $$$. But, my codes are over 10,000 lines long. And I knew there had to be a better way.

Fast forward to today where I built a main file, and put a class in another file with some basic math functions. Then in my main file I built a little GUI to solve simple math problems. (I LOVE PYSIMPLEGUI, a big shout out to u/MikeTheWatchGuy for all the work he does.) And I just imported the class into my main and called it as normal...and it works. Easy as pie. I couldn't believe it. I feel like I've just learned a super valuable piece of information that I had been missing out on. Milestone achieved.

Thanks for reading and sharing in my epiphany!

433 Upvotes

75 comments sorted by

View all comments

2

u/Prof_P30 Nov 25 '20

I know using globals is a bad thing to do, but how to avoid it? I want to display program name and version in different scripts of my program. But defining only once. How do you do that?

3

u/MikeTheWatchGuy Nov 25 '20

Make a function that returns those values. When a part of you programs wants the info, call the function.

4

u/MacItaly if MacItaly: return helpful / else: return to_Google Nov 25 '20

Here he is! I am still humbled by your continued generosity of knowledge and willingness to share and help others grow within their coding journey. You've helped me on Reddit, Github, and via email. I thank you again for your assistance, and for your tireless work in developing PySimpleGUI.

1

u/MikeTheWatchGuy Nov 25 '20

☺I'm just a developer guy like many others here. Lots of people want to help.

It's nice that you're grateful for the help. Gratitude and happiness are linked, so I hope you're as happy as you sound grateful. I appreciate the recognition, but it's not needed. Seeing what other people create using my stuff is enough recognition.

2

u/MacItaly if MacItaly: return helpful / else: return to_Google Nov 25 '20

I wish I could give you a tutorial on how I've used your stuff to build my programs. It's been a huge benefit to my company, and to my personal growth as a developer. And there's something about building a quality program that boosts self-esteem and self-efficacy. I am indeed happy and grateful.

1

u/Prof_P30 Nov 25 '20

Thanks.

And what to do with thousands of these, which I user over all my scripts/modules in dialogs, main windows, widgets across my projects.

const.py:

...

ICON_ADD = "./img/icons8-add-new-50.png"ICON_EDIT = "./img/icons8-edit-file-50.png"ICON_DEL = "./img/icons8-cancel-50.png"

...

Making a function for every single entry seems a bit of an overdose?

1

u/MikeTheWatchGuy Nov 25 '20

One way could be to use a dictionary that's filled with these values. I've seen some programs use a class to hold constants.

1

u/MikeTheWatchGuy Nov 25 '20

This is what I meant when I said using a class to hold constants. The class doesn't have to be instantiated in order to get the values. This way it's not actually a variable taking up space as a global.

class consts():
    SIZE = (5, 3)
    BUTTON_NAME = 'Go'

print(f'the size is {consts.SIZE}, button name is {consts.BUTTON_NAME}')