A strange phenomenon! What is this number 8! We are in the 2.7
version of Python.

**MY CODE
**

x = raw_input("write a list! utilisation of raw_input ")
y = input("write a list! utilisation of input ")

print x, "resultat of rawinputation"
print y, "resultat of inputation"

**My input and the Terminal
**

write a list! utilisation of raw_input [x * 3 for x in range(2, 10, 2)]
write a list! utilisation of input [x * 3 for x in range(2, 10, 2)]
8 resultat of rawinputation
[6, 12, 18, 24] resultat of inputation

**The lesson:
**
a) an important discover

  • with input python make return the result of the list comprehention

b) an astonishment:

  • with raw_input python return 8.

A QUESTION:

What is this 8?

Recommended Answers

All 12 Replies

When [x * 3 for x in range(2, 10, 2)] is evaluated as a result of the input function, the variable x takes the values 2, 4, 6, 8. When the x is printed, its value is the last one, 8. You can avoid this by using another variable name.

Conclusion: don't use input() in python 2, it is too dangerous.

@gribouiilis
Thank so much!
Your answer generate a second generation of questions.
What input make is clear. copying the output and pasting it in the code.
But raw input is an hybrid action. When I read the following console:

write a list! utilisation of raw_input 2 + 5
write a list! utilisation of input 2 + 5
2 + 5 result of rawinputation
7 result of inputation

or:

x1 = raw_input("write a number x1 ! utilisation of raw_input ")
x2 = raw_input("write a number x2 ! utilisation of raw_input ")

y = input("write an addition! utilisation of input ")

print x1, " result of rawinputation"
print y, " result of inputation"
print x1 + x2, " addition of rawinputations"


-------> in console <--------


write a number x1 ! utilisation of raw_input 3
write a number x2 ! utilisation of raw_input 4
write an addition! utilisation of input 3 + 4
3  result of rawinputation
7  result of inputation
34  addition of rawinputations

I believe that It past the input as a string.
But in the first example 8 is not a conversion of comprehention to string but an hybrid action. It is even a biggest astonishment.

SECUNDO
What is so threatening with input?

With Python2 input() uses eval() internally.
The eval function can potentially evaluate a statement to erase your harddrive.

With Python3 the former input() has been replaced with the functionallity of raw_input().

@vegasteat
Thank you for the accurate response!

Sorry if i don't have the technical level to understand enough.
You says that python 2's input() function makes the attribution eval() action, that is:
1- the input is a string
2- the eval function read the characters into the quotes as the interpreter read a code
3- the eval function return the same thing as if in the interpreter we write the inner-quotes characters, and type enter.

**
4- But in this way I don't understand what can we erase.

5- If the raw_input() function is a simple 'string-macker', WHAT IS THE FAMOUS "8"?**

Between the raw_input() and your printing x, there was an input(). This is where x was modified.

About the dangers of input(), the point is that when a program writes write a list!, the user does not think that his input can have a malicious effect such as erasing files or starting unwanted programs, etc. There should be some mechanism in your program to prevent this from happening. Python's eval() function is too powerful, or it must be used in conjonction with code restricting the string that can be passed to input().

An example is given in this snippet where an expression is carefully analysed before being given to eval(), thus allowing only the evaluation of mathematical expressions.

@gribouillis
Yes, your answer is phenomelogically verified:

x = raw_input("write a list! utilisation of raw_input ")
print x, "result of rawinputation"
y = input("write a list! utilisation of input ")

print y, "result of inputation"


............> <-----------------
write a list! utilisation of raw_input [x * 5 for x in range(2, 10, 2)]
[x * 5 for x in range(2, 10, 2)] resultat of rawinputation
write a list! utilisation of input [x * 5 for x in range(2, 10, 2)]
[10, 20, 30, 40] resultat of inputation

But the astonishment is growing.

WHY the input between lines 1 and 3 of the former snippet interfere, his purpose is an other thing!
Now it is very interesting!

If you input [z * 5 for z in range(2, 10, 2)], it does not interfere.

@Gribouillis

OK! You give me a bud of understanding.
The name of the variable inside the input are names od global variables, not as in a functions.

The last value of x in the liste inside the input function is 8.

UNTIL NOW I can guess what is the 8.

And if I write print x, the so called x variable is called;
the computer will attribute to x this last value!

Fantastic, Gribouiilis! The question is SOLVED.

Actually, in Python2, unlike Python3, the list comprehension leaks. So in this case x (as its last value) becomes available outside the comprehension.

@vegaseat
If I understand your comment,

I can learn through him that list comprehension has a "function behaviour":
______________________________________________________________________
IN THE SNIPPET I PRESENTED AT BEGINING,
There was a SECOND PROBLEM, beyond the awkward homonymy between the x in line 1 (i.e. x = raw_input etc) and the x into the inputed [x * 3 for x in range(2, 10,20)].

Normally, the content of the name of variables into the list comprehension was supposed to not affect a global variable (and that is the case in Python 3).

            _____________________

But the present language is python 2.

And python 2 do not manage list comprehension as a function,

            _____________________

In consequence the awkward homonymy is truly infortunate!

A short test ...

# Python2
mylist = [x * 3 for x in range(2, 10, 2)]

# x leaks out and contains 8, the last value given to it
print(x)

... and ...

# Python3
mylist = [x * 3 for x in range(2, 10, 2)]

# x does not leak out
# gives NameError: name 'x' is not defined
print(x)

@vegaseat
Awesome!
Thank's so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.