Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: A better way to accomplish loop

Thread Tools

Re: A better way to accomplish loop

Dave Angel
Posts: n/a
On 02/12/2013 02:59 PM, Joseph L. Casale wrote:
> I have an issue with some code I have been passed:

I had to read it about four times before I knew what you were saying.
Maybe I still have it wrong.

> for (x, y) in [(a_dict1, a_tuple[0]), (a_dict2, a_tuple[1])]:
> I only noticed it as PyCharm failed to assign the str type to y, whereas it knew
> the tuples 0 and 1 item were type str.

I think you're saying that the lint-feature of PyCharm is trying to
guess the object types, and telling you there's a conflict here. I
don't think you're saying that it executes incorrectly.

> In the loop it flags the passing of y into a method that expects type str. I can ignore
> it, but looking at the loop, I cant help but think there is a better way?

By better, you could have meant
1) clearer for the reader
2) runs faster or takes less memory
3) fools PyCharm into guessing better in its checking.

If it's #3, then I'm no real help, as I've never seen a paper on the
philosophy of the PyCharm guesser.

Still there are ways to express it differently, and maybe one of them
will happen to please PyCharm.

Name the variables to represent what they're holding. Those names might
also imply type, though I wouldn't normally go out of my way to
accomplish it. But if the two parts of the two tuple are strings,
perhaps the tuple as a whole might be called names, or titles, or
authors. However, generally a tuple is NOT expected to have the same
type for all its elements.

for x,y in zip((a_dict1, a_dict2), a_tuple):

dicts = [a_dict1, a_dict2]
for x,y in zip(dicts, a_tuple):

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
RE: A better way to accomplish loop Joseph L. Casale Python 0 02-12-2013 11:01 PM
A better way to accomplish loop Joseph L. Casale Python 0 02-12-2013 07:59 PM
Standard way to accomplish production / development switch kellygreer1 ASP .Net Web Services 1 09-27-2006 05:28 PM
XML: Better way to accomplish this? flamesrock Python 2 01-04-2005 12:08 AM