0

I am trying to create a function that accepts a variable, and optionally a data type and a prompt as arguments, then calls input() and assigns the input to the variable in the arguments.

def inp(var, prompt='', intype=str):
    var = intype(input(prompt))
    return None

Now, if I do this (assuming name x is already defined):

inp(x,'hello',int)

I want to print "hello" (that part already works) then convert the input received into an int and store it in variable x.

However, it does not seem to work. (the required value is not assigned to the variable passed as argument). I am aware that my code is awkward, so how can I make it work?

  • 1
    You should return the new value from the function and assign the result. – Daniel Roseman Nov 02 '19 at 11:41
  • 2
    Possible duplicate of [How do I pass a variable by reference?](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – Carcigenicate Nov 02 '19 at 11:42
  • But ya, you likely shouldn't be doing this. Functions make more sense when they return their input. – Carcigenicate Nov 02 '19 at 11:43
  • @Carcigenicate I would never use this for any actual use, it's only an experiment. – randomname123 Nov 02 '19 at 11:44
  • 1
    See also [Why can a function modify some arguments as perceived by the caller, but not others?](https://stackoverflow.com/q/575196/12892) and [Modifying function arguments](https://stackoverflow.com/q/45111055/12892) – Cristian Ciupitu Nov 02 '19 at 11:44
  • @randomname123 Then long story short, you'll need to package it in a mutable object and mutate the object. There isn't a way to do literally what you're asking afaik. – Carcigenicate Nov 02 '19 at 11:45
  • @Daniel Roseman I agree that is better, but is this possible? – randomname123 Nov 02 '19 at 11:45
  • Yes I think it is if you use reflection, ill give it a go aswell, or possibly through a decorator... – Har Nov 02 '19 at 11:47

2 Answers2

0

You can modify your code as shown below to achieve the desired result

def inp(prompt,intype):
    var = intype(input(prompt))
    return var

x = inp("Hello",float)
print(x)

You can also check this thread (How do I pass a variable by reference?) as mentioned in one of the comments

0

If you did the following it would work, however you will have to change your function signature slightly:

def inp(variableName, prompt='', inttype="str"):
    operation = "global {variable}; {variable} = {cast}(input(prompt))"
    exec(operation.format(variable=variableName, cast=inttype, prompt=prompt))

So you would be required to modify the method to accept strings as opposed to the actual values.

Therefore if you wanted to modify the variable x you will be required to pass in the string "x"

This sort of wraps exec within a function.

In [19]:     def inp(variableName, prompt='', inttype="str"):
    ...:         operation = "global {variable}; {variable} = {cast}(input(prompt))"
    ...:         exec(operation.format(variable=variableName, cast=inttype, prompt=prompt))
    ...:

In [20]: inp("x", prompt="hello world", inttype="str")
hello worldbye!

In [21]: print(x)
bye!

Lesson is that python is a dynamic language and anything is possible :) (however I wouldn't recommend this)

Har
  • 3,727
  • 10
  • 41
  • 75