Functions depending on other functions in Python [closed] - python

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 14 days ago.
Improve this question
def mean(x):
return(sum(x)/len(x))
def variance(x):
x_mean = mean(x)
return sum((x-x_mean)**2)/(len(x)-1)
def standard_deviation(x):
return math.sqrt(variance(x))
The functions above build on each other. They depend on the previous function. What is a good way to implement this in Python? Should I use a class which has these functions? Are there other options?

Because they are widely applicable, keep them as they are
Many parts of a program may need to calculate these statistics, and it will save wordiness to not have to get them out of a class. Moreover, the functions actually don't need any class-stored data: they would simply be static methods of a class. (Which in the old days, we would have simply called "functions"!)
If they needed to store internal information to work correctly, that is a good reason to put them into a class
The advantage in that case is that it is more obvious to the programmer what information is being shared. Moreover, you might want to create two or more instances that had different sets of shared data. That is not the case here.

Related

Can dividing code too much make it inefficient? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
If code is divided into too many segments, can this make the program slow?
For example - Creating a separate file for just a single function.
In my case, I'm using Python, and suppose there are two functions that I need in the main.py file. If I placed them in different files (just containing the function).
(Suppose) Also, If I'm using the same library for the two functions and I've divided the functions into separate files.
How can this affect efficiency? (Machine performance-wise and Team-wise).
It depends on the language, the framework you use etc. However, dividing the code too much can make it unreadable, which is (most of the time) the bigger problem. Since most of the time you will (or should) be working in a team, you should consider how readable your code would be for them.
However, answering this in a definite way is difficult. You should ask a Senior developer on your team for guidelines.

Why is list comprehension so prevalent in python? [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
Often you see question asked about a better method of doing something, or just generally a looping question and very often the top answers will use some form of convoluted list/dict/tuple comprehension that takes longer for others to understand than create themselves. While a simple and understandable loop could have just been made.
Since it cannot provide any speed benefits that I could imagine, is there any use of it in python other than to look smart or be Pythonic?
Thanks.
I believe the goal in this case to make your code as concise and efficient as possible. At times it can seem convoluted, but the computer looping through multiple lines as opposed to a single line adds processing time, which in large applications and across many iterations can cause some delays.
Additionally, although it seems harder to understand initially, for an outside individual reading your code, it's much quicker for them to read simplified expressions than pages of loops to get an idea of what you're attempting to accomplish.

Array vs object - what's faster in Python [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 4 years ago.
Improve this question
I am wondering what I should do for the purpose of my project.
I am gonna operate on about 100 000 rows, every time.
what I wanted to do is to create an object "{}" and then, if I need to search for a value, just call it , for example
data['2018']['09']['Marketing']['AccountName']
the second option is to pull everyting into an array "[]" and in case I need to pull value, I will create a function to go through the array and sum numbers for specific parameters.
But don't know which method is faster.
Will be thankful if you can shed some light on this
Thanks in advance,
If performance (speed) is an issue, Python might not be the ideal choice...
Otherwise:
Might I suggest the use of a proper database, such as SQLLite (which comes shipped with Python).
And maybe SQLAlchemy as an abstraction layer. (https://docs.sqlalchemy.org/en/latest/orm/tutorial.html)
After all, they were made exactly for this kind of tasks.
If that seems overkill: Have a look at Pandas.

What would differ between these simple codes? [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 4 years ago.
Improve this question
So I've been kinda new to some concepts, can someone please briefly explain what is the difference between these two codes?
regressor=LinearRegression()
regressor.fit(train_X,train_Y)
.
LinearRegression().fit(train_X,train_Y)
The main difference between the two is that the first creates a variable called regressor which you can later access. The second doesn't do this.
Otherwise the two are doing exactly the same thing.
The purpose of fitting (training) the regressor is to use it in the future for prediction. In you r second example (LinearRegression().fit(train_X,train_Y)) you create an anonymous regressor, train it, and then immediately discard. You cannot use it anymore as it does not have any references.
In the first example, you first create a regressor and assign it to a variable, then train the regressor that was previously created. You can later use it for prediction or any other purpose.

How to get all methods/functions that contain a particular string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get all methods/functions available that contain a particular string? For ex., use *csv* to get all the method/function details which have the csv text in them.
The above is for the sake of learning, each class supports a lot of methods. I would like to filter and narrow down the list of methods, so that I can explore them. Same is the case with functions also, for the sake of learning.
You can examine the globals() dictionary:
[x for x,y in globals().items() if 'csv' in x and callable(y)]
If you only wanna functions, you can do something like this:
[x for x,y in globals().iteritems() if 'csv' in x and y.__class__.__name__ == 'function']
but if you want also classes I recommend you to use Rob's way.

Categories