Python issue when returning function from another file
Question: I'm having a bit of an issue when trying to return a function from another
file.

main.py:

    
    
    from master_input import load_input
    
    class Vera(object):
        def __init__(self):
            masterinput = load_input()
            self.masterinput = masterinput
    
        def load_masterinput(self):
            return self.masterinput
    

master_input.py:

    
    
    import sys
    
    def load_input():
        if sys.version_info <= (3,0,0):
            masterinput = raw_input()
        elif sys.version_info >= (2,7,11):
            masterinput = input()
        return masterinput
    

There seems to be no output when running the first file. I want it to return
`masterinput` in the second file because if I were to end the function with
`load_input()`, in the second file, it would just output without even
returning `self.masterinput` in the first file.


Answer: You don't show an example of instantiated one of the **Vera()** options nor to
you show any methods that would be using (displaying/printing or otherwise
manipulating) this **masterinput** attribute of your **Veta()** instance.

So it seems quite likely that your code doesn't seem to be "doing" anything.
You'd declared what the objects look like, how to instantiate them and how to
respond to a (poorly named) _load_masterinput()_ method call.

Also your module isn't returning a function. When an object is instantiated
it, that could will be returning a string, the result of calling either the
_input()_ or _raw_input()_ built-in function.

By the way, the smart way, in my opinion, to handle the change from Python2.x
_raw_input()_ to Python3 _input()_ is to use code like this:

    
    
    #!python
    if 'raw_input' in dir(__builtins__):
        input = raw_input
    

Note that I'm assigning the function _raw_input_ to the name _input_ ... I'm
NOT calling the function and assigning the results of its evaluation here.

Then all my code can use _input()_ and we can forget that the Python2
(evaluated) _input()_ semantics ever existed.

If I want to actually return a function from a function here's one way to do
so:

    
    
    #!python
    # filename: my_input.py
    def get_input_function():
        if 'raw_input' in dir(__builtins__):
            # Python2.x and earlier
            return raw_input
        else:
            # Python3
            return input
    

... I would then call this something like so:

    
    
    #!python
    import my_input
    input = my_input.get_input_function()
    

This is unnecessarily obtuse. There's no reason to do something like this for
something so trivial. There are cases where you might imagine calling some
function, with certain arguments and returning different functions based on
those arguments. However, in most cases, you'd still be better off creating a
**_class_** and instantiating an instance of that.

