Skip to content

Replaces print statement with built in function#428

Merged
ddemidov merged 2 commits intoev3dev:developfrom
moriarty:python-print-statement
Oct 29, 2017
Merged

Replaces print statement with built in function#428
ddemidov merged 2 commits intoev3dev:developfrom
moriarty:python-print-statement

Conversation

@moriarty
Copy link
Copy Markdown
Contributor

The print statement was removed with Python3, this change should work
for both Python2 and Python3.

The print(i),(ax) looks a little strange though.

I just ran an out-of-the-box sonar analysis on the project and found this: it wasn't actually causing me any issue.

The print statement was removed with Python3, this change should work
for both Python2 and Python3.
@moriarty
Copy link
Copy Markdown
Contributor Author

Before:
bugs
After:
fixed

(note- ignore the unresolved in the "after" photo... the other project I have in sonar is the PointCloudLibrary)

@moriarty
Copy link
Copy Markdown
Contributor Author

I tried print(i, ax) and it gave me a different output

@moriarty
Copy link
Copy Markdown
Contributor Author

test = { "hello", "world"}

for i, st in enumerate(test):
    print i, st

for i, st in enumerate(test):
    print(i, st)

for i, st in enumerate(test):
    print(i),(st)

@moriarty
Copy link
Copy Markdown
Contributor Author

It's possible my test was wrong.

@ddemidov
Copy link
Copy Markdown
Member

ddemidov commented Oct 29, 2017

print(i),(ax) is a tuple of whatever is returned by print(i) (spoiler: None) and ax:

>>> print(1),(2)
1
(None, 2)

@ddemidov
Copy link
Copy Markdown
Member

$ python2
Python 2.7.14 (default, Sep 20 2017, 01:25:59) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 1, 2
1 2
$ python3
Python 3.6.2 (default, Jul 20 2017, 03:52:27) 
[GCC 7.1.1 20170630] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(1,2)
1 2

fixed from code review
@moriarty
Copy link
Copy Markdown
Contributor Author

I've pushed a new commit with print(i, ax)

@moriarty
Copy link
Copy Markdown
Contributor Author

alex@localhost:~/workspace/ev3dev-lang-python> python
Python 3.6.3 (default, Oct 11 2017, 14:49:33) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> test = {"hello", "world"}
>>> for i,st in enumerate(test):
...   print(i, st)
... 
0 hello
1 world
$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> test = {"hello", "world"}
>>> for i,st in enumerate(test):
...     print(i, st)
... 
(0, 'world')
(1, 'hello')

Strange, on my MacBook I get a different result, but it's also a different version of Python than on my server.

@ddemidov
Copy link
Copy Markdown
Member

ddemidov commented Oct 29, 2017

You are using print(...) version on python2 here, which (I guess) prints the tuple (i, st). The brackets are part of tuple in the second example.

$ python2
Python 2.7.14 (default, Sep 20 2017, 01:25:59) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print (1,2)
(1, 2)
>>> print 1, 2
1 2

@ddemidov
Copy link
Copy Markdown
Member

>>> print('{} {}'.format(1, 2)) should give same results both for python2 and python3, but we don't support python2 anyway, so I don't see why would we want to be backward compatible.

@moriarty
Copy link
Copy Markdown
Contributor Author

yes that print('{} {}'.format(1, 2)) doesn't look nice 😄

@ddemidov ddemidov merged commit bc8e5a0 into ev3dev:develop Oct 29, 2017
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

Successfully merging this pull request may close these issues.

2 participants