Whenever the input() function is used, the computer reads any input() variables and stores them in memory. Print()'s sole function is to display the result of input() after given operations.
Update:
In Python, an argument is also stored, but it requires a function such as print() to operate. The computer is analogous to a sheet of paper…
Q2:
In this example, what makes the output ‘’ instead of '***’?
A = {'key1': input('*'), 'key2': input, 'p1': print, 'p2': print}
while A ['key1']('*') is true:
A ['p1']('***')
Please don’t respond at great length (to save your time) I just need to check if my understanding is correct.
The input function blocks (waits) until the user types something and hits Enter/Return. It then returns the string typed by the user. In the assignment above, what the user typed is assigned to the variable called response. Here I type the string “this test” and then hit Return:
>>> response = input(">> type something: ")
this test
>>> response
'this test'
>>> print(response)
this test
>>> print("123 " + response + " 456")
123 this test 456
There is no magical connection between input and print. The print function has no knowledge of input or of whatever is returned by input. You have to tell print each time what you want to print, by providing a string (or something printable) as argument.
It seems you are fascinated by dicts… If you put this code in a file and try to run it, then you will see you will get an error after you type something. When Python runs this script and builds up dictionary A, it will call the input function with prompt ‘*’. It will wait, and when you then type something, it will assign the typed string to A["key1"]. Next you will get an error, since A['key1']('*') is trying to use that string as if it’s a function, and strings are not callable.
Modify the code, by adding another print statement:
and at the prompt type a fulll word, not just enter. That will perhaps make this clearer…
Even though your dict definition is formally valid, you will really never see code like that (where dictionary values are builtin functions). (There are use-cases where something a little bit like this could make sense – for instance if you were coding up a python compiler in python or something similar – but in “regular” programs you’ll never see this. Even so, using input in the way you did never makes sense since it’s a function intended for interactive use, reading from stdin, in some command-line application.)
You know, there are a ton of free tutorials for Python where these things are explained – usually pretty clearly. See, for instance (just a random choice): Take input from stdin in Python - GeeksforGeeks
You might consider reading up on that.
Yes, at least temporarily. A dict() is similar to a set(), but set() seems more limited to me; for example, it cannot write a function within a set(). <<< Is it true? I haven’t tried.
To me, dict() can describe anything…
You’re awesome. Thanks again… I will re-read and think about all the previous comments you made.
If you want something to challenge yourself a bit, have a look at
Try out the examples, then inspect the internal “registry” field of the function:
from functools import singledispatch
@singledispatch
def fun(arg, verbose=False):
if verbose:
print("Let me just say,", end=" ")
print(arg)
Make sure to also define one or two of the others ‘dispatches’ mentioned in the docs. Then do:
>>> dir(fun)
# will show you accessible members of fun
>>> fun.registry
# will show something like this - which is basically a kind of dict
mappingproxy({<class 'object'>: <function fun at 0x1028d8a60>, <class 'int'>: <function _ at 0x10292c940>, <class 'list'>: <function _ at 0x10292c9d0>})
So, you can do
>>> fun.registry[int]
<function _ at 0x10292c940>
>>> fun.registry[int](42)
42
>>> fun.registry[int](42, verbose=True)
Strength in numbers, eh? 42
Of course in normal usage, you’d call fun(42), but that call is essentially forwarded to fun.registry[int].