Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print() for tuple should use print() to stringify its contents #10233

Closed
mkyt opened this issue Feb 18, 2015 · 13 comments
Closed

print() for tuple should use print() to stringify its contents #10233

mkyt opened this issue Feb 18, 2015 · 13 comments

Comments

@mkyt
Copy link

mkyt commented Feb 18, 2015

When printing tuple, its contents are currently stringified by show() instead of print().

julia> a::Uint16 = 0x4321
0x4321
julia> print(a)
17185
julia> print((a, ))
(0x4321,)

I think it is preferable to use print for displaying its contents when printing tuple
as print and show give different results for some types, as in the example above.

@mkyt mkyt changed the title print() for tuples should use print() to stringify its contents print() for tuple should use print() to stringify its contents Feb 18, 2015
@JeffBezanson
Copy link
Member

The reason we do this is that numbers have canonical printed representations, but tuples do not. As soon as we're printing ( ,), we're using julia syntax and might as well stick with it. If we printed (17185,), that would be a representation of a julia object, but of a different julia object, which seems like a strange thing to do.

@kmsquire
Copy link
Member

This suggests (again) a python-like distinction between repr and string.

@StefanKarpinski
Copy link
Member

It's is a bit messier than ideal right now, but there is a somewhat consistent distinction made already.

@mkyt
Copy link
Author

mkyt commented Feb 19, 2015

I cannot grasp what "canonical" means in this context, but as I read the discussion in #3450,
I thought show/repr and print/string in julia roughly correspond to python's repr (unambiguous) and str (human-friendly) and so it is more natural and consistent that these distinction propagate to their contents when showing or printing container objects (like tuples).

@JeffBezanson
Copy link
Member

By "canonical" I mean not julia-specific. I don't really see the value of printing (17185,) here. Who/what is the consumer for that output?

@mkyt
Copy link
Author

mkyt commented Feb 19, 2015

Hex notation is quite annoying when debugging codes dealing with tuples containing unsigned ints.

julia> ui, ui_t = uint(123), tuple(uint(456), uint(789))
(0x000000000000007b,(0x00000000000001c8,0x0000000000000315))
julia> show(ui)
0x000000000000007b
julia> print(ui)
123
julia> show(ui_t)
(0x00000000000001c8,0x0000000000000315)
julia> print(ui_t)
(0x00000000000001c8,0x0000000000000315) # (456,789) is much more comprehensible

@jiahao
Copy link
Member

jiahao commented Feb 19, 2015

Relevant: #7959

@ihnorton ihnorton added the io Involving the I/O subsystem: libuv, read, write, etc. label Feb 19, 2015
@nalimilan
Copy link
Member

Even if Julia syntax is used for printing tuples, it doesn't mean the output should be parseable to the same Julia object, if the goal is to offer a short/readable representation (the intended consumer of print is a human).

@johnmyleswhite
Copy link
Member

-1 to printing unsigned ints as integers rather than hex

@nalimilan
Copy link
Member

@johnmyleswhite I think the debate here is: why would print((0x1,)) use hex while print(0x1) wouldn't, and vice-versa? I.e. a consistency issue.

@StefanKarpinski
Copy link
Member

Let's just print everything in hex – consistency problem solved.

@JeffBezanson
Copy link
Member

I can totally understand not wanting to see hex in many cases, but I think that's the only real issue here. Consider another example of the general proposal described by the issue title:

julia> print("hello, world.")
hello, world.
julia> print(("hello, world.",))
("hello, world.",)

With the proposed change, the second output would be (hello, world.,). Is that what you want?

@JeffBezanson JeffBezanson removed the io Involving the I/O subsystem: libuv, read, write, etc. label Feb 20, 2015
@mkyt
Copy link
Author

mkyt commented Feb 23, 2015

@JeffBezanson, thanks for pointing this out. I wasn't aware of the pitfall.

On second thought, I realized the current behavior of print is "consistent" in that
it uses canonical notation if any, uses repr otherwise, as @StefanKarpinski stated
and it would be better to leave it as it is.

Nevertheless, I think its' very handy to have a easy way of controlling over the level
of detail in printing julia objects out in the planned clean-up of output functions (#7959).

@mkyt mkyt closed this as completed Feb 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants