Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Added idioms to style.rst #52

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

Merged
merged 2 commits into from
Apr 27, 2012
Merged

Added idioms to style.rst #52

merged 2 commits into from
Apr 27, 2012

Conversation

epequeno
Copy link
Contributor

Not sure what the section on idioms was supposed to contain.

The first idiom using str.join() I've seen used a lot and has been recommended in several places as being better than using a for loop to concatenate strings to an already existing string.

The 2nd idiom is to use dictionaries when you need to perform lookups. I consider it idiomatic because it's not obvious that you should exploit the hashtable lookup this way. In this example I mapped from a string to an empty list but mapping to any arbitrary thing (None, False, '', etc.) would work equally as well.

It seems to me that the dict lookup should go under the idioms section rather than performance.

@@ -7,6 +7,29 @@ Idioms

Idiomatic Python code is often referred to as being *pythonic*.

A common idiom for creating strings is to use `join <http://docs.python.org/library/string.html#string.join>`_ on an empty string.::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably easier to set up intersphinx with CPython and then just link in using that: http://sphinx.pocoo.org/latest/ext/intersphinx.html

@epequeno
Copy link
Contributor Author

Thanks for the feedback. I agree that these examples may not fit well in the idioms section. I wasn't exactly sure what the intent of the section was so I just tried those to see if @kennethreitz could give any guidance about what he expected.

I like the idea of having a recipe section to isolate best-practices given common problems.

@dcrosta
Copy link
Contributor

dcrosta commented Dec 31, 2011

When I think of idiomatic Python, I think of things that make sense in other languages which look "odd" in Python. A naive port of C (or PHP or Java or ...) code for a for loop might look like this:

things = ['one', 'two', 'three']
for i in range(len(things)):
    print things[i]

whereas idiomatic/Pythonic code would be simply:

for thing in things:
    print thing

Taking it a step further, suppose we also need access to the list indices:

for i, thing in enumerate(things):
    print i, thing

There are lots of other examples, I'm sure. A few others off the top of my head: list/tuple unpacking, and variable swaps (i.e. a, b = b, a); using in rather than has_key etc; defaultdict; list/set/dict comprehensions and generator expressions; as well as some more advanced examples like how to write decorators, how to write decorators that take arguments, and how to write decorators that optionally take arguments (I'm actually not sure how to do the third, but I suppose it's probably possible). This could grow to be a very long section, indeed, so some editorial effort may be required to document the most common or most important, and link to existing references for others.

@kennethreitz
Copy link
Contributor

I think this would work quite well in a performance section.

@epequeno
Copy link
Contributor Author

tbqh, I'm not sure where to go from here. Should I close the pull request and wait for for you to make a performance section?

I understand where you're coming from as far as moving this to a performance section. I'm new to making pull requests and how the direction of a project like this should progress. I'd like to know what your thoughts on moving forward are.

@dcrosta
Copy link
Contributor

dcrosta commented Jan 1, 2012

I'd suggest:

  1. Rework this pull request into a Performance Tips section (which I don't think exists, so you'll have to add one)
  2. If you want, do some research on sites on the internet listing Python idoms, pick those you think are most important, and add those to the Idomatic Python section (with links back to the source sites as necessary)

@kennethreitz
Copy link
Contributor

This is fantastic stuff. Sorry for the delay!

kennethreitz pushed a commit that referenced this pull request Apr 27, 2012
Added idioms to style.rst
@kennethreitz kennethreitz merged commit 8311ad2 into realpython:master Apr 27, 2012
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.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.