What does this comma do "print (Blah,)"

41 views
Skip to first unread message

jettam

unread,
Sep 21, 2017, 11:27:19 PM9/21/17
to Python Programming for Autodesk Maya
I have a question about adding a comma to the end of a these print statements.   In the first example  ( print (Blah))  with out the comma, it prints the string?   and in the second example where I added the comma ( print (Blah,) ) It turns it into a tuple?   Is my observation correct here? 

Blah = "the comma changes it into, a tuple!?"
print (Blah)
# result: the comma changes it into, a tuple!?


Blah = "the comma changes it into, a tuple!?"
print (Blah,)
# result: ('the comma changes it into, a tuple!?',)


Simon Anderson

unread,
Sep 22, 2017, 12:24:16 AM9/22/17
to Python Programming for Autodesk Maya
That is correct. From my understanding, print will query each objects _repr_ which it stores in a tuple and then prints that tuple out. 
The reason why the single value is not a tuple, is that you cannot create a tuple with one value.

ie variableA = (), or variableA = (1) should both fail
if you want to create a tuple with only one value you have to write variableA = (1,)

Justin Israel

unread,
Sep 22, 2017, 1:59:37 AM9/22/17
to python_in...@googlegroups.com
On Fri, Sep 22, 2017 at 4:24 PM Simon Anderson <simonben...@gmail.com> wrote:
That is correct. From my understanding, print will query each objects _repr_ which it stores in a tuple and then prints that tuple out. 
The reason why the single value is not a tuple, is that you cannot create a tuple with one value.

ie variableA = (), or variableA = (1) should both fail
if you want to create a tuple with only one value you have to write variableA = (1,)

The print statement (python 2.x) does not create a tuple. It is printing the repr of whatever you give it. 

The syntax being used is actually creating a tuple:
tup = (1,)
 

On Friday, 22 September 2017 13:27:19 UTC+10, jettam wrote:
I have a question about adding a comma to the end of a these print statements.   In the first example  ( print (Blah))  with out the comma, it prints the string?   and in the second example where I added the comma ( print (Blah,) ) It turns it into a tuple?   Is my observation correct here? 

Blah = "the comma changes it into, a tuple!?"
print (Blah)
# result: the comma changes it into, a tuple!?


Blah = "the comma changes it into, a tuple!?"
print (Blah,)
# result: ('the comma changes it into, a tuple!?',)


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2a74e97c-21f1-4fed-a2fc-42b2d54d06fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Sep 22, 2017, 2:34:30 AM9/22/17
to python_in...@googlegroups.com

Does it really print the repr, assuming we’re talking about the __repr__ method of its class instantiation? I could have sworn it printed the __str__ and had to double check.

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information

>>> class MyClass(object):
...   def __repr__(self):
...     return "My __repr__"
...   def __str__(self):
...     return "My __str__"
...
>>> print(MyClass)
<class '__main__.MyClass'>
>>> print(MyClass())
My __str__
>>> print(repr(MyClass()))
My __repr__
>>>

On 22 September 2017 at 06:59, Justin Israel <justin...@gmail.com> wrote:
On Fri, Sep 22, 2017 at 4:24 PM Simon Anderson <simonben...@gmail.com> wrote:
That is correct. From my understanding, print will query each objects _repr_ which it stores in a tuple and then prints that tuple out. 
The reason why the single value is not a tuple, is that you cannot create a tuple with one value.

ie variableA = (), or variableA = (1) should both fail
if you want to create a tuple with only one value you have to write variableA = (1,)

The print statement (python 2.x) does not create a tuple. It is printing the repr of whatever you give it. 

The syntax being used is actually creating a tuple:
tup = (1,)

On Friday, 22 September 2017 13:27:19 UTC+10, jettam wrote:
I have a question about adding a comma to the end of a these print statements.   In the first example  ( print (Blah))  with out the comma, it prints the string?   and in the second example where I added the comma ( print (Blah,) ) It turns it into a tuple?   Is my observation correct here? 

Blah = "the comma changes it into, a tuple!?"
print (Blah)
# result: the comma changes it into, a tuple!?


Blah = "the comma changes it into, a tuple!?"
print (Blah,)
# result: ('the comma changes it into, a tuple!?',)


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2sDjkEeOjoDW6F8ksAGUepCUxDsZhsi5q1JfJ9Ypbe6Q%40mail.gmail.com.

jettam

unread,
Sep 22, 2017, 2:56:59 AM9/22/17
to Python Programming for Autodesk Maya
 I am getting lost in the weeds here.   But I think my take away is that by putting a comma " print (Blah,) " will turn this into a tuple.

Thanks in advance..

Simon Anderson

unread,
Sep 22, 2017, 3:04:34 AM9/22/17
to Python Programming for Autodesk Maya
From my understanding, str is a string representation, that one uses if they want to perform comparisons with other strings.

If you create the str method it will trump the repr method when printing. repr is usualy used to show what the object represents, but wont be used in evaluations.

class str(object=”)

Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable toeval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.

For more information on strings see Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange which describes sequence functionality (strings are sequences), and also the string-specific methods described in the String Methods section. To output formatted strings use template strings or the % operator described in the String Formatting Operations section. In addition see the String Services section. See also unicode().


repr(object)

Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.

Reply all
Reply to author
Forward
0 new messages