Is it possible to have input as a default value for a function?
For example,
f(x=input('>')):
print(x)
The problem is, it makes me input a value even when I supply an x value. How would I do this correctly?
Python will evaluate the default arguments you define for a function, you'll get the prompt during definition either you like it or not. The value entered at the input prompt will then be assigned as the default for x.
If you want a default value that will use input if no other meaningful value is provided, use the common idiom involving None and drop input in the body of the function, instead:
def f(x=None):
if x is None:
x = input('> ')
print(x)
Now, if you're thinking, I still want to somehow do this with default arguments, then you'll need to create a function that re-creates your function with every invocation:
def f():
def _(x = input('> ')):
print(x)
return _()
I don't see what benefit this would bring other than appeasing your curiosity, though :-)
May be this is what you want.
def f(x=None):
if not x:
x = input('>')
print(x)
Related
In Python, what value can a variable take, so that when a function is invoked with the variable as an argument, the function uses its default value for the parameter instead?
Consider the following code:
def foo(a=100):
print(a)
b = None #blank value
foo(b)
Desired output:
100
Actual output:
None
I hypothesized that None would work, but clearly it doesn't. What value can I choose for b, so that foo(b) is equivalent to foo()? Or is this simply not possible? I'm in a situation where the value for b can either be defined, or I would like to use the default value of the parameter.
(This answer assumes that you cannot modify foo, and that you cannot use reflection or introspection to determine what the default argument value is.)
It's the absence of an argument, not any particular value used as an argument, that triggers the use of the default value. The only way you can produce nothing out of something is to unpack an empty mapping
foo(**{})
or an empty sequence
foo(*())
Both * and ** are part of the function-call syntax, though, not part of the argument value, so with a variable, it still looks like
b = {}
foo(**b)
b = ()
foo(*b)
If you want None to revert to a default value, the easiest way is to do the logic in the function itself.
def foo(a=None):
if a is None:
a = 100
print(a)
Function default parameter uses when there don't pass any parameter for the argument. And None is not a blank value. None is an object of NoneType Datatype in python similar to Other Datatype Object.
Instead, you can use
def foo(a=100):
a=100 if a is None else a //Ternary operator
print(a)
b = None #blank value
foo(b)
Output:
100
In my case, I ended up using the inspect module to create a helper function which extracts the default values of the function as described here:
import inspect
def get_defaults(func):
signature = inspect.signature(func)
return { k: v.default for k, v
in signature.parameters.items()
if v.default is not inspect.Parameter.empty }
def foo(a=100):
print(a)
b = get_defaults(foo)['a']
foo(b)
Output:
100
Let's assume I have two functions
def seq():
#here I wrote a code that evaluates the mean of a value from a csv file
print(x)#assuming the condition in the above code is true it prints x
seq()
and
def lenn():
p=4
d=#I want this variable to be the value that the 1st function produces
x=d/p
lenn()
One produces an integer and the other uses the output of the 1st function and then divides it with an integer to produce its own output. How do I call the function?
I tried calling the function name but when I tried to divide the function name with an integer it keeps saying that I have a None type. I also tried to put the 1st first function inside the 2nd function but I had the same problem.
How can i solve this?
Don't use print but return (print has no return value, so this defaults to None):
def seq():
return int(input())
def lenn():
p=4
d=seq()
x=d/p
return x
print(lenn())
The problem is that seq does not return the inputted value (x). Anyway, I wouldn't place int(input(x)) in its own function. You can try something like
def lenn():
p=4
d=int(input())
x=d/p
return x
I'm learning python, and I'm having trouble saving the return of a function to a specific variable. I have computed a simple function 'average' that is supposed to return the average value of a list. This works fine, however, when I try to store the result of average in a variable, I get told that x isn't defined.
def average(x):
return sum(x)/len(x)
var=average(x)
How do I store the return of the function in a variable?
Edit:
I misunderstood the task, which was simply to store the results of a specific computation in a variable.
x indeed is not defined
def average(x):
return sum(x)/len(x)
x = [1,2,3] # this was missing
var=average(x)
https://repl.it/join/eldrjqcr-datamafia
The function is a black box. You made the black box expect one mandatory input (x), therefore you have to provide it first i.e. var = average([1, 2, 3]).
Read the error message, x isn't defined. The variable x exists only in the average function and not in the program. You need to set x to something first.
e.g.
def average(x):
return sum(x)/len(x)
x=[1,2,3]
var=average(x)
this will not cause an error
I have a function in my code:
X = []
Y = [1,2,3,4]
class DATA():
def __init__(self):
X.append(Y)
DATA()
print (X)
When I run my code, I want this class (named DATA) to be implemented only when I press the Enter key. Any idea how I can do it? (A simple example is appreciated!).
This was hard to answer for a couple reasons:
You keep using the word "implement", but I'm pretty sure you mean "call" instead. I'm going to assume that here, since conditionally implementing something is rarely needed.
Your use of the class here is confusing. It isn't actually needed in this case, so it's just complicating things. I'm going to reduce it down to just:
def DATA():
X.append(Y)
Since that does the same thing as your existing code.
To ask for user input, use input. input waits until the user presses Enter, then returns the typed text. You can use it here to wait for Enter to be pressed:
x = []
y = [1,2,3,4]
def data():
x.append(y)
print("x before Enter is pressed:")
print(x)
input("Type Enter to continue...")
data() #Call DATA similar to your original code
print("x after Enter is pressed:")
print(x)
A couple extra notes:
Function and variables names should be in lower case. Uppercase is reserved for class names, and even in those cases, only the first letter is uppercase.
Ideally, you shouldn't be mutating global variables like x and y. They should be passed into the function, then the result should be returned.
I am trying to make a decrypter that decrypts code from the encrypter I made. I am getting this type error when I run the code though
getcrypt = ''.join(map(Decrypt.get,split_up_into_sixteen_chars(x_str)))
TypeError: split_up_into_sixteen_cjars() takes 0 positional arguments but 1 was given
I'm fairly new to programming and not sure whats causing this.
heres my code
Decrypt = {'1s25FF5ML10IF7aC' : 'A', 1s2afF5ML10I7ac' : 'a'} #I obviously have more than this but I'm trying to make it as simplified as possible
def split_up_into_sixteen_chars():
while len(x_str)>0:
v = x_str[:16]
print(v)
x_str = (input())
getcrypt = ''.join(map(Decrypt.get,split_up_into_sixteen_chars(x_str)))
print(getcrypt)
You have defined a function that takes no parameters:
def split_up_into_sixteen_chars():
yet you are passing it one:
split_up_into_sixteen_chars(x_str)
You need to tell Python that the function takes one parameter here, and name it:
def split_up_into_sixteen_chars(x_str):
The name used does not have to match the name that you pass in for the function call, but it does have to match what you use inside the function. The following function would also work; all I did was rename the parameter:
def split_up_into_sixteen_chars(some_string):
while len(some_string) > 0:
v = some_string[:16]
print(v)
This works because the parameter some_string becomes a local name, local to the function. It only exists inside of the function, and is gone again once the function completes.
Note that your function creates an infinite loop; the length of some_string will either always be 0, or always be longer than 0. The length does not change in the body of the loop.
The following would work better:
def split_up_into_sixteen_chars(some_string):
while len(some_string) > 0:
v = some_string[:16]
print(v)
some_string = some_string[16:]
because then we replace some_string with a shorter version of itself each time.
Your next problem is that the function doesn't return anything; Python then takes a default return value of None. Printing is something else entirely, print() writes the data to your console or IDE, but the caller of the function does not get to read that information.
In this case, you really want a generator function, and use yield. Generator functions return information in chunks; you can ask a generator for the next chunk one by one, and that is exactly what map() would do. Change the function to:
def split_up_into_sixteen_chars(some_string):
while len(some_string) > 0:
v = some_string[:16]
yield v
some_string = some_string[16:]
or even:
def split_up_into_sixteen_chars(some_string):
while some_string:
yield some_string[:16]
some_string = some_string[16:]
because an empty string is 'false-y' when it comes to boolean tests as used by while and if.
As your map(Decrypt.get, ...) stands, if split_up_into_sixteen_chars() yields anything that is not present as a key in Dycrypt, a None is produced (the default value for dict.get() if the key is not there), and ''.join() won't like that. The latter method can only handle strings.
One option would be to return a string default instead:
''.join(map(lambda chunk: Decrypt.get(chunk, ''), split_up_into_sixteen_chars(x_str)))
Now '', the empty string, is returned for chunks that are not present in Decrypt. This makes the whole script work for whatever string input you have:
>>> x_str='Hello world!'
>>> ''.join(map(lambda chunk: Decrypt.get(chunk, ''), split_up_into_sixteen_chars(x_str)))
''
>>> x_str = '1s25FF5ML10IF7aC'
>>> ''.join(map(lambda chunk: Decrypt.get(chunk, ''), split_up_into_sixteen_chars(x_str)))
'A'