organizing large python object [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a python object that represents a connection to a rest api with a large number of endpoints. I need to come up with a strategy to organize hundreds of method calls (right now I am using prefixes on the methods). I would also like to break up the files if that's possible.
I am working in python 2.7
Two give more clear questions -
1) Is it possible to split an object into multiple files in some sort of effective manner in python? Specifically without introducing more complexity?
2) Is it possible to make "submodules" for lack of a better phrase, so
my_rest_api.users.create
rather than
my_rest_api.users_create

You can implement the groups as mixins. For example like this:
user_actions.py
class UserActions:
def user_create(...): ...
the_api.py
from . import user_actions
class TheApi(UserActions, ItemActions, OtherActions, ...):
# common functionality goes here
Then you can call the_api_instance.user_create(...) once you have an object created.

Related

Is there a better solution for manipulating one value across multiple functions other than global variables? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I’m creating a text based adventure game in Python and for the the state of character and amount of one item I’m using global variables, but I’ve heard it’s not the best solution and I can’t seem to find one solid answer.
You have heard correctly. Global variables should always be avoided.
For the purposes of your project, I recommend you look into Object Orientated Programming (OOP) in Python. This is a better way of keeping track of the state of objects in your program.

Python Best Practice: Better to use nested functions or private function syntax? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I made a package that I've refactored following Uncle Bob's clean code book.
The result: I have a ton of smaller functions from the giant functions I had before (as I'm still learning Python & best practices).
My question is this:
To keep things organized...
On functions you won't use other places...
is it better to use nested functions
def public_func():
def private_func():
# do abc
private_func()
# do xyz
or have a lot of smaller functions with the private function syntax?
def _private_func():
# do abc
def public_func():
_private_func()
# do xyz
As the Zen of Python says:
Flat is better than nested.
But also:
Simple is better than complex.
Complex is better than complicated.
Therefore, try to not use nested, but if it makes it more complicated, keep it simple.

Should modules always contain a class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a module which only contains functions. Is it good practice to put these inside a class, even if there are no class arguments and the __init__ function is pointless? And if so how should I write it?
There's no particular reason to force functions to go inside a class if there's no reason to make a class. A python sourcefile already provide a perfectly reasonable namespace for storing similar, related functions. The pypy implementation of the python programming language, for example, has many modules that don't have classes in them (for example, time).
You would want a certain data structure as a class, of course, but there are certain behaviors that are entirely independent of data structures, and putting them in a class would just add needless complexity.
It is good to build modules that contain a class for better organization and manipulation depending on how big the code is and how it will be used, but yes it is good to get use to building classes with methods in them. Can you post your code?

What are the possible applications of deque functions in real time use? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Help me learn with the application of real time use of deque functions from collection library, if possible try adding some examples to it .
Thank You.
the docs for deque itself nominates two examples, given that maxlen is provided:
Bounded length deques provide functionality similar to the tail filter in Unix. They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest.

What is the difference between python function and python module [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am a beginner of python i didn't found any difference between function and module.it is said that module stores code even after shutting the shell off rather than function.but when i tried to do so it didn't work for me.SO what is the big deal of using a module rather than function in the programming
In programming, function refers to a segment that groups code to perform a specific task.
A module is a software component or part of a program that contains one or more routines.
That means, functions are groups of code, and modules are groups of classes and functions.

Categories

Resources