You are not logged in.

#1 2014-02-15 01:02:48

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,346

General Python Style Question

Often, I need to make a couple trips through a loop, but do not need to reference to the number of times I have been through the loop.  Example:

                for i in range(2):
                    location = self.Move(location,options[direction])

Here, I have an array of directions I can move, and I want to move two spaces an a given direction.  To do this, I want to make two moves.  I have no need of the index i.  Is there a more Pythonic way of doing this where I do not need to declare a variable I never use? 

Edit: location is a  2nd order Tuple, options is a Tuple of vectors (2nd order Tuples that are added element by element to location), direction is an integer

Last edited by ewaller (2014-02-15 01:07:39)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#2 2014-02-15 01:07:21

progandy
Member
Registered: 2012-05-17
Posts: 5,280

Re: General Python Style Question

Unused variables often get only an underscore as the name. This convention may be used by some python interpreters to improve the execution.

Last edited by progandy (2014-02-15 01:15:13)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#3 2014-02-15 01:10:22

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,346

Re: General Python Style Question

Python 3.3.3 (default, Nov 26 2013, 13:33:18) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for _ in range (2): print ("foo")
... 
foo
foo
>>> for _ in range (2): print ("foo"): print (_)
  File "<stdin>", line 1
    for _ in range (2): print ("foo"): print (_)
                                     ^
SyntaxError: invalid syntax
>>> 

Interesting.  Thank you.

edit:

>>> for _ in range (2): print ("foo") print (_)
  File "<stdin>", line 1
    for _ in range (2): print ("foo") print (_)
                                          ^
SyntaxError: invalid syntax
>>> for _ in range (2): print (_)              
... 
0
1
>>> 

  Oh, not so interesting, but still interesting smile

Last edited by ewaller (2014-02-15 01:13:02)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#4 2014-02-15 01:15:39

progandy
Member
Registered: 2012-05-17
Posts: 5,280

Re: General Python Style Question

If you want to improve speed, use itertools.repeat(None, times) instead of range(times), it is a bit faster because it doesn't have to generate new python objects but returns the same "None" every time.
Edit: In python2, you have also xrange (return a generator) and range (returns a list). In python3 range was removed and xrange renamed to range.

Last edited by progandy (2014-02-15 01:17:42)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#5 2014-02-15 01:19:47

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,346

Re: General Python Style Question

That is exactly what I was looking for smile  Thanks!


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB