Posts: 2,953
Threads: 48
Joined: Sep 2016
(Oct-19-2016, 07:28 AM)buran Wrote: (Oct-19-2016, 06:51 AM)Skaperen Wrote: true, but that's not the challenge. the challenge is to get code that does the same thing now in python3 and keep it simple. how simple can it be done in python3? is this the best case?
print('()') ?
well, I start to be confused about all this. If you want the python 3 code to print empty tuple, then it is as follows
print(()) Output: Python 3.5.2 (default, Jul 17 2016, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print (())
()
>>>
I don't understand why the complications with printing the str '()'. If you want to print empty tuple, do it...
The original questions was regarding unexpected behaviour using 2to3 tool
Is it tuple? How to address it? It hardly that type(()) should work
Posts: 8,172
Threads: 160
Joined: Sep 2016
Is it tuple? - What else do you think it is? :-)
Posts: 1,298
Threads: 38
Joined: Sep 2016
Oct-19-2016, 01:40 PM
(This post was last modified: Oct-19-2016, 01:50 PM by sparkz_alot.)
(Oct-19-2016, 12:56 PM)buran Wrote: Is it tuple? - What else do you think it is? :-)
Well,
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = print(())
()
>>> type(a)
<class 'NoneType'>
>>> b = print(),
>>> type(b)
<class 'tuple'>
>>>
>>> c = print(()),
()
>>> type(c)
<class 'tuple'>
>>>
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 8,172
Threads: 160
Joined: Sep 2016
Oct-19-2016, 03:23 PM
(This post was last modified: Oct-19-2016, 03:30 PM by buran.)
(Oct-19-2016, 01:40 PM)sparkz_alot Wrote: (Oct-19-2016, 12:56 PM)buran Wrote: Is it tuple? - What else do you think it is? :-)
Well,
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = print(())
()
>>> type(a)
<class 'NoneType'>
>>> b = print(),
>>> type(b)
<class 'tuple'>
>>>
>>> c = print(()),
()
>>> type(c)
<class 'tuple'>
>>>
In your example a is None, because that is the default return value of each function.
b and c are single element tuples, because of that comma you add at the end of the line. Note that first and only element of the both is exactly the same None returned by the print function.
Hope the following example helps
Output: Python 3.5.2 (default, Jul 17 2016, 00:00:00)
[GCC 4.8.4] on linuxType "help", "copyright", "credits" or "license" for more information.
>>> a = print(())()
>>> print(print(()))
()
None
>>> b = print(),
>>> print(b)
(None,)
>>> c = print(()),
()
>>> print(c)
(None,)
Note the code
print(print(())) and the output
Output: ()
None
inner print function prints the tuple, then the outer functions prints the return value from the inner one - i.e. None
same as in this
Output: >>> print(print('something'))
something
None
>>>
Posts: 1,298
Threads: 38
Joined: Sep 2016
Yes, I understand, I was pointing out the the example "print(())" is actually not a tuple
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 8,172
Threads: 160
Joined: Sep 2016
(Oct-19-2016, 03:36 PM)sparkz_alot Wrote: Yes, I understand, I was pointing out the the example "print(())" is actually not a tuple
the output, i.e.
Output: ()
IS EXACTLY the string representation of the tuple class (empty tuple instance to be precise)
Posts: 1,298
Threads: 38
Joined: Sep 2016
(Oct-19-2016, 03:53 PM)buran Wrote: (Oct-19-2016, 03:36 PM)sparkz_alot Wrote: Yes, I understand, I was pointing out the the example "print(())" is actually not a tuple
the output, i.e.
Output: ()
IS EXACTLY the string representation of the tuple class (empty tuple instance to be precise)
Logically, one would think so, but Python does not. As shown in my example, Python see it as class 'NoneType', not class 'tuple'
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 8,172
Threads: 160
Joined: Sep 2016
Oct-19-2016, 04:15 PM
(This post was last modified: Oct-19-2016, 04:42 PM by buran.)
(Oct-19-2016, 04:11 PM)sparkz_alot Wrote: (Oct-19-2016, 03:53 PM)buran Wrote: (Oct-19-2016, 03:36 PM)sparkz_alot Wrote: Yes, I understand, I was pointing out the the example "print(())" is actually not a tuple
the output, i.e.
Output: ()
IS EXACTLY the string representation of the tuple class (empty tuple instance to be precise)
Logically, one would think so, but Python does not. As shown in my example, Python see it as class 'NoneType', not class 'tuple'
You DO make difference between the type of the argument you pass to the print function and the type of the return value of the function, right? If you don't make that difference, then I don't see how I can explain it better
Output: >>> type(print('something'))
something
<class 'NoneType'>
You would not say that 'something' is of NoneType, right?
Posts: 2,168
Threads: 35
Joined: Sep 2016
b is a tuple
>>> b = ()
>>> a = print(b)
()
>>> type(a), type(b)
(<class 'NoneType'>, <class 'tuple'>)
Posts: 7,329
Threads: 123
Joined: Sep 2016
Oct-19-2016, 07:35 PM
(This post was last modified: Oct-19-2016, 09:11 PM by snippsat.)
(Oct-19-2016, 04:11 PM)sparkz_alot Wrote: Yes, I understand, I was pointing out the the example "print(())" is actually not a tuple
Logically, one would think so, but Python does not. As shown in my example, Python see it as class 'NoneType', not class 'tuple' print function is returning a tuple,but it's in stdout which is not catch in the variable.
Variable catch only return type from function.
The print function has no return value def print(*args, sep=' ', end='\n', file=None)
print function is not meant to be stored in a variable,no return in function.
If look at it in an another way,foo is returning an integer but we only catch None('NoneType').
>>> def foo():
... print(999)
...
>>> a = foo()
999
>>> repr(a)
'None'
>>> type(a)
<class 'NoneType'
|