This question already has answers here: How do I import other Python files? (23 answers) Closed 6 months ago. file.py contains a function named function. How do I import it? The above gives an error: ImportError: No module named ‘file.py’; file is not a package Answer First, import function from file.py: Later, call the function using: Note that file is
Tag: function
python max function using ‘key’ and lambda expression
I come from OOP background and trying to learn python. I am using the max function which uses a lambda expression to return the instance of type Player having maximum totalScore among the list players. The function correctly returns instance of type Player having maximum totalScore. I am confused about the following three things: How does the max function work?
Passing an integer by reference in Python
How can I pass an integer by reference in Python? I want to modify the value of a variable that I am passing to the function. I have read that everything in Python is pass by value, but there has to be an easy trick. For example, in Java you could pass the reference types of Integer, Long, etc. How
How to check whether optional function parameter is set
Is there an easy way in Python to check whether the value of an optional parameter comes from its default value, or because the user has set it explicitly at the function call? Answer Lot of answers have little pieces of the full info, so I’d like to bring it all together with my favourite pattern(s). default value is a
How do I find the difference between two values without knowing which is larger?
I was wondering if there was a function built into Python that can determine the distance between two rational numbers but without me telling it which number is larger. e.g. Obviously I could write a simple definition to calculate which is larger and then just do a simple subtraction: but I’d rather not have to call a custom function like
Why do function objects evaluate to True in python?
In python it is valid to make a construction like: I wish to ask what is the logic that a function pointer is evaluated to True. Why was this kind of construction inserted in the language? Answer A lot of things evaluate to True in Python. From the documentation on Boolean operators: In the context of Boolean operations, and also
Using a dictionary to select function to execute
I am trying to use functional programming to create a dictionary containing a key and a function to execute: Now, I have seen a code used to find the defined functions in a module, and I need to do something like this: So my question is, How do I make a list of all the Exec functions and then assign
How to get the return value from a thread?
The function foo below returns a string ‘foo’. How can I get the value ‘foo’ which is returned from the thread’s target? The “one obvious way to do it”, shown above, doesn’t work: thread.join() returned None. Answer In Python 3.2+, stdlib concurrent.futures module provides a higher level API to threading, including passing return values or exceptions from a worker thread
How can I make a deepcopy of a function in Python?
I would like to make a deepcopy of a function in Python. The copy module is not helpful, according to the documentation, which says: This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged;
What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)? [duplicate]
This question already has answers here: return, return None, and no return at all? (5 answers) Closed 4 months ago. Let’s assume an iteration in which we call a function without a return value. The way I think my program should behave is explained in this pseudocode: If I implement this in python, it bothers me, that the function returns