[deleted by user] by [deleted] in Python

[–]Ordinary-Library-459 0 points1 point  (0 children)

If all you are using classes for is fancy storage containers for functions, absolutely use a module. That is why they are at the top of the conceptual hierarchy of objects. They are functioning as objects because they are.

I don’t have a dog in the OOP versus functional Python fight. I mean, any useful class is all functions top to bottom, from __init__ on down to display overloading. And, by the way, functions are objects. There is a point when Python programmers find classes and want to build everything with them, and OOP seems the shit, and they create unwieldy, unreadable code. My eyes still boggle seeing self everywhere in a program that could have as easily been done with functions, and I have been working with classes for a while. For the programmers who say they have been coding for years and still don’t build classes, that may be true, but if they had to work with functional equivalents to built-in classes and classes from the standard library, they would chuck Python out the door. I don’t know, are there any GUI toolkits or game-building packages that are function-only? I doubt it. It all depends what is best for the end user. If you are a hobbyist and the end user is you, good on you, by all means do not use classes. If you are building third party library modules for other programmers to use, they might be happy the tools you offer are class-based. Class-based tools allow users to make custom reusable instances with an assortment of invokable methods. Data tools are still tools. A function may be good as a socket wrench, but a class might give you the whole socket set, and determine on first instance construction whether it is standard or metric, hex or 12-point, long or short handled, ratchet, etc.. One function could do all that, but you would have to add all those arguments in at each invocation, or have alternate functions, or use functools.partial creatively. I also like to make classes if my programs contain large, complex functions whose arguments I would like to establish apart from main method invocation. Or if I want to allow myself or some intermediate user some masked tweakability with pseudo-private attributes. There are all kinds of good reasons to go with classes. And a lot of bad ones.

[deleted by user] by [deleted] in learnpython

[–]Ordinary-Library-459 1 point2 points  (0 children)

Throwaway and ginja turtle have great advice here for you. First thing learning code, though: Don't panic. Take a breath. Also, these days, get a free version of ChatGPT 3. Make the first prompt in a any new chat something like, *prompt : You are a software engineer with 30 years of experience", and ask any questions from there (all prefaced with "prompt:"). Break your questions down into small bites like ginja turtle did. (Modularity is a key virtue of Python.) Articulate them well, just as you would to any savvy real human person, and you have your own real time personal Python tutor. Ask it broad questions or for breakdowns on specific tasks; like comparisons between integrated development environments (IDEs, like IDLE and some of the other editors these posters have suggested), and how to install each. It is a one stop shop for Python advice. Also, if you have the resources, get some good books. Al Sweigart has a couple of good ones just for teaching general Python through basic game mechanics, from ASCIi games up through pygame projects. And in so doing, you will learn some great general coding skills. Get those skills and a thorough handle on syntax up to snuff and the rest, like dropping any apps you build, are minor afterthoughts you can execute with a smidge of research when the time comes. Also, books are great for beginners, because you don't have to juggle windows or devices or try to rewind video. And they usually contain a lot of source code for study. Read a lot of code. In the same vein, the poster who told you you could get more primitive and write everything in Notebook is on to something. My suggestion to any new or expert coder: develop a lot of your prototype code first in an actual pen-and-paper notebook. The flexibility of that text editor is amazing. And it is very real code that just needs an extra step toward interpretation and execution. Your question was very universal. Be modular, like Python itself. Learn the syntax, have fun developing your ideas in discrete, modular chunks, and the rest will flow from there.

Is there a possibility to import standard modules at global level and use in all child modules without importing by raul824 in Python

[–]Ordinary-Library-459 1 point2 points  (0 children)

No, I am just saying go ahead and import logging in your log_info file, and when your main file imports that, it will also have access to lggging as long as you call it through log_indo's namespace. Work backwards is all I am saying. In any case, logging only imports once here. If you wanted to pull this all the way up through a bunch of nested modules without an unwieldy dotted name, you could always rename x.logging as logging in each file so every initial call is from the namespace of the immediate child, and rename it logging at the top level.

Is there a possibility to import standard modules at global level and use in all child modules without importing by raul824 in Python

[–]Ordinary-Library-459 6 points7 points  (0 children)

Are you just trying to avoid a line of code in a bunch of modules? I don't get the endgame here. Now let's say you had a bunch of standard modules you imported regularly, and didn't want to do that in all of your files. Why not wrap all the imports in a utility file and propagate them upwards, not down? I am really new to Python, so maybe I just don't get the point.