classification
Title: type() and len() recognize "abc", expression as "abc" string.
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, mrabarnett, rhettinger, umedoblock
Priority: normal Keywords:

Created on 2017-04-17 04:43 by umedoblock, last changed 2017-04-17 04:55 by rhettinger. This issue is now closed.

Messages (2)
msg291781 - (view) Author: umedoblock (umedoblock) Date: 2017-04-17 04:43
But I found a real bug to use a tuple with a comma.

Python3 recognized "abc", expression as tuple of one element.
But type() and len() recognize "abc", expression as "abc" string.
So now, I found a real bug.

I'll show you below sentences.

>>> "abc",
('abc',)
>>> obj = "abc",
>>> obj
('abc',)
>>> type(obj)
<class 'tuple'>
>>> len(("abc",))
1
>>> len(obj)
1

>>> type("abc",)
<class 'str'>
>>> len("abc",)
3
msg291782 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-04-17 04:55
What you're seeing is a feature of the grammar.

Function allow an optional trailing comma in the argument list:

    >>> pow(2, 5)
    32
    >>> pow(2, 5,)
    32

So, to create a tuple inside in an argument list, you need the extra layer of parentheses.

Sorry, this isn't a real bug.  It is just a conflict resolution between use of a comma to separate arguments in a function call and use of a comma to create a tuple.
History
Date User Action Args
2017-04-17 04:55:21rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg291782

resolution: not a bug
stage: resolved
2017-04-17 04:43:18umedoblockcreate