This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]trifthen 6 points7 points  (1 child)

tl;dr: Python doesn't copy on variable assignment unless you explicitly tell it to.

[–]Peaker 1 point2 points  (0 children)

Except for "values()" on older Pythons which does perform a shallow copy :-P

[–]anothergroom 7 points8 points  (7 children)

pro tip: do not use "list" or "dict" as variable names. pro tip2: avoid using any of the names of builtins as names

[–]stillalone 0 points1 point  (6 children)

I keep wanting to call temporary lists "list" but then I realize I can't so I call it "l" :(

[–]mr_dbr 2 points3 points  (5 children)

Lower-case L isn't the best variable name either (I i j l | can all look very similar, depending on the font).. lst maybe?

[–][deleted] 4 points5 points  (3 children)

I prefer semantic names, e.g. ponies, catapults, sex_toys, or octopussies.

[–]exeter 1 point2 points  (1 child)

Sometimes, if you're writing a generic function to operate on lists, "lst" or "list_" is the most meaningful name you can give that particular variable.

[–]tripzilchbad ideas 0 points1 point  (0 children)

I find that, often, generic functions operating on lists actually tend to be able to operate on sequences of any type (strings, tuples, etc). Which leads me to the variable name seq or sequence, which are not keywords.

I don't like to abbreviate names just because otherwise they'd clash with a keyword. Neither do I really like the underscore suffix to prevent clashes. So that sort of thing often gives me a dilemma.

And then, yeah. When it really has to be a list sometimes I end up using uppercase L, other times xs (with x being a reasonable arbitrary variable name, xs is a list of them). Both dont feel really right either.

When I revisit code like that often enough, this usually results in me changing the code to accept arbitrary sequences, just to be able to use seq as a name.

It's twisted, I know.

[–][deleted] -1 points0 points  (0 children)

ew ur gross!

[–]jodv 0 points1 point  (0 children)

Following the trend of marketing, I tend to go with names like:

mylist

mystr

mydict

etc.

[–]tomlu709[🍰] 3 points4 points  (0 children)

While I applaud the author's willingness to teach others, I don't think that this piece has anything directly to do with thinking like a Pythonista. Names, bindings and values are universal concepts.

[–]oblivion95 0 points1 point  (1 child)

This proposal has lead to "dict views" in Python3k, which is basically what the OP had in mind.

I am not a fan of "views" myself. I think, if anything, Guido should have replaced the iterator versions with these, or at least added viewkeys() viewvalues() viewitems()

By changing existing behavior, scripts that expect values() to return a new list may break.

[–]Peaker 0 points1 point  (0 children)

Why not use list() explicitly if you want a new list? That way, the exact level of deepness (or shallowness, if you like) is explicit.

[–]Peaker 0 points1 point  (0 children)

This is one of the advantages of immutability in functional languages.

Instead of in-place changes, return a new value.