Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
QuarkJets
Sep 8, 2008

Eela6 posted:

Thanks! I picked up those tricks from Luciano Ramalho's Fluent Python and Brett Slatkin's Effective Python, respectively.

I am writing a speech for my local python developers group.

It's called 'The Elements of Style.' It's still in the works, but my rough draft is something like 80% complete. I would love advice on the slides. Take a look and let me know what you think!

http://tinyurl.com/sdpystyle

"Functions should have as few arguments as possible" gets introduced on slide 41 but it probably belongs in the list of Function rules on slides 21-26. This is basically just another function rule, is it not?

I think "needless identifiers" is a misnomer; _ is just a variable name like anything else. I understand that some people like to use _ to indicate "this variable is not used" but that's still an identifier. I'm not sure what to actually call it

Adbot
ADBOT LOVES YOU

Eela6
May 25, 2007
Shredded Hen
Thank you. Good eye on slide 41 --> 27

Re: 'omit needless identifiers' - it's mostly there as a take on Strunk & White's famous 'omit needless words'. If I can come up with a better way to phrase the concept I will. '_' is just a variable name, but it's also a well-known Python idiom for 'don't worry about it'. It allows the reader to focus on the important identifiers.

I also realized that this is way, way too long, so I'm going to split it into three more focused talks and try and give one a month. I tried 'giving' the speech in the car while stuck in traffic and it went over 40 minutes. Considering I have a 15-20 minute time slot, this needs a lot of cutting down. Thank you for your input.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
The use of _ to mean "this is an unused variable" is bad. Don't tell people to do that. If you need a variable that's unused, use the word "unused".

Cingulate
Oct 23, 2012

by Fluffdaddy

Eela6 posted:

Thanks! I picked up those tricks from Luciano Ramalho's Fluent Python and Brett Slatkin's Effective Python, respectively.

I am writing a speech for my local python developers group.

It's called 'The Elements of Style.' It's still in the works, but my rough draft is something like 80% complete. I would love advice on the slides. Take a look and let me know what you think!

http://tinyurl.com/sdpystyle
:eng101:

`@timed` probably isn't a (simple) past tense verb, but a past participle here (possible resultative passives? not sure). (i.e., I assume you'd say @eaten vs. @ate?)

`get_current_time()` is a verbal phrase, not just a verb.

In principle, class names are deverbal nominalizations, specifically agent nouns. But I guess that's the one where people really stop caring.

Sorry, I'm a linguist

Eela6
May 25, 2007
Shredded Hen
Thank you everyone for your feedback.

Cingulate posted:

:eng101:

`@timed` probably isn't a (simple) past tense verb, but a past participle here (possible resultative passives? not sure). (i.e., I assume you'd say @eaten vs. @ate?)

`get_current_time()` is a verbal phrase, not just a verb.

In principle, class names are deverbal nominalizations, specifically agent nouns. But I guess that's the one where people really stop caring.

Sorry, I'm a linguist

Thanks! I am sure you are right. I don't understand some of what you are talking about, so I will read more about parts of speech and correct my errors.

You are dead right about @eaten vs @ate. I would use "eaten".

Examples of "good" decorator names, from my point of view:
code:
@logged
@cached
@timed
@compiled

Obviously I wouldnt say "eated", I would say "eaten", but I'm having trouble thinking of other words that have that form change without the -ed ending.

Hammerite posted:

The use of _ to mean "this is an unused variable" is bad. Don't tell people to do that. If you need a variable that's unused, use the word "unused".

I'm sorry, I disagree. It's a matter of... style. :rimshot: It is a super common python convention, recognized by most. IDEs. More importantly, I find the alternative visually noisy.

Compare
Python code:

_, green, _ = pixel
unused, green, unused = pixel 

If you still disagree that's fine, I certainly think it'd better to explicitly say "unused" rather than just have meaningless identifiers. But I like the underscore!

Eela6 fucked around with this message at 15:29 on Feb 1, 2017

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

Eela6 posted:

Thanks! I am sure you are right. I don't understand some of what you are talking about, so I will read more about parts of speech and correct my errors.

You are dead right about @eaten vs @ate. I would use "eaten".


I'm sorry, I disagree. It's a matter of... style. :rimshot: It is a super common python convention, recognized by most. ides. More importantly, I find the alternative visually noisy.

Compare
Python code:
_, green, _ = pixel
unused, green, unused = pixel 
If you still disagree that's fine, I certainly think it'd better to explicitly say "unused" rather than just have meaningless identifiers. But I like the underscore!

It's also convention in (at least) Ruby and Elixir as well as Python.

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!
Pretty much every language with pattern matching / tuple destructuring uses _ as the "ignore this, just a placeholder" value.

Cingulate
Oct 23, 2012

by Fluffdaddy

Eela6 posted:

Obviously I wouldnt say "eated", I would say "eaten", but I'm having trouble thinking of other words that have that form change without the -ed ending.
I'm not an expert on English historical morphology, so take this with a gram of salt, but: I think this is a typical case of regularization. For ordinary and for rare words, we "save mental RAM" and make learning easier by forming both the past and the participle with a simple rule (adding an -ed). For more common verbs, we much more often allow us more RAM usage - maybe for faster access (a functional explanation), maybe it's just that since they are so common, we haven't felt the optimization pressure yet and kept a traditional form - and for these very frequent verbs, we use irregular forms.

See - saw - seen
Speak - spoke - spoken
Throw - threw - thrown

Etc

But most modern and most rare words should be regular. E.g. calculate, email etc.

No Safe Word
Feb 26, 2005

Hammerite posted:

The use of _ to mean "this is an unused variable" is bad. Don't tell people to do that. If you need a variable that's unused, use the word "unused".

Good lord no, _ as "ignore me" is convention. Naming a variable "unused" is worse than just naming it the intentionally-awkward-and-already-established-as-conventional name.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
I actually recently had this discussion at my job. I've been using _ in my code here and there (in Python) at work without anyone saying anything. Then, recently in a review, it was brought up that our only sr engineer didn't know why I was using it. I mentioned it was just convention for unused, and he told me it was not and that I should just prepend a variable name with an underscore if it wasn't going to be used. I was about to bring up that this would probably be confusing, since it is convention in Python to prepend things with underscores to denote that they are "private", not that they are unused, but then instead I gave up.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

The March Hare posted:

and he told me it was not

welp

accipter
Sep 12, 2003
What are peoples thoughts on unicode variable names (e.g., Φ)? I do a lot of programming of equations that might have variables without physical meaning (no proper name) and are just a greek symbol. I will usually use an ascii representation of that variable because it is simpler to enter, but it is a little removed from the original reference.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

Yep, already brushing up on my binary trees and scoping new work. Not just for that, obviously, but it is indicative of the work environment. Not really where I want to be as a jr.

Also, I assume ^ are joking, but why not just "phi"?

Proteus Jones
Feb 28, 2013



accipter posted:

What are peoples thoughts on unicode variable names (e.g., Φ)? I do a lot of programming of equations that might have variables without physical meaning (no proper name) and are just a greek symbol. I will usually use an ascii representation of that variable because it is simpler to enter, but it is a little removed from the original reference.

If you're working in Python3 you should be fine, since source file encoding is UTF-8.

If you're using 2.x, I think you need to declare the encoding like this (not 100%, I had to look it up)

Python code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
However, it's on you to make sure your editor supports it and is not inserting extra crap.

Proteus Jones fucked around with this message at 18:52 on Feb 1, 2017

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

accipter posted:

What are peoples thoughts on unicode variable names (e.g., Φ)? I do a lot of programming of equations that might have variables without physical meaning (no proper name) and are just a greek symbol. I will usually use an ascii representation of that variable because it is simpler to enter, but it is a little removed from the original reference.

I dunno...if I had to figure out how to type such a thing when working with your code, I'd hate you forever.

However, I rarely do anything requiring me to type anything other than whats on my US keyboard. Maybe if your code is only ever going to be used by math people who routinely use unicode math symbols it'd be ok?

accipter
Sep 12, 2003

flosofl posted:

If you're working in Python3 you should be fine, since source file encoding is UTF-8.

If you're using 2.x, I think you need to declare the encoding like this (not 100%, I had to look it up)

Python code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
However, it's on you to make sure your editor supports it and is not inserting extra crap.

I know that it is possible. I am just wondering if it makes any sense.

Thermopyle posted:

I dunno...if I had to figure out how to type such a thing when working with your code, I'd hate you forever.

However, I rarely do anything requiring me to type anything other than whats on my US keyboard. Maybe if your code is only ever going to be used by math people who routinely use unicode math symbols it'd be ok?

This is my basic thought. Entering something that is not on the keyboard is painful so I have avoided it in the past.

a witch
Jan 12, 2017

I would avoid it if it would cause any friction with someone else who might use your code.

a witch
Jan 12, 2017

a witch posted:

I would avoid it if it would cause any friction with someone else who might use your code.

Please also note that "someone else" might be you in six months.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

No Safe Word posted:

Good lord no, _ as "ignore me" is convention. Naming a variable "unused" is worse than just naming it the intentionally-awkward-and-already-established-as-conventional name.

It's a bad convention. It's unnecessarily cryptic, and fails to be self-explanatory. On the other hand if you name a variable "unused", that's self-explanatory.

Not only these objections, but also _ is used in the Python command line interpreter to refer to the object last evaluated, so it's giving something two different meanings for no good reason.

Space Kablooey
May 6, 2009


_ is less garbage to read than unused.

Eela6
May 25, 2007
Shredded Hen

accipter posted:

What are peoples thoughts on unicode variable names (e.g., Φ)? I do a lot of programming of equations that might have variables without physical meaning (no proper name) and are just a greek symbol. I will usually use an ascii representation of that variable because it is simpler to enter, but it is a little removed from the original reference.

I think it's almost always a mistake to use non-english native language identifiers. I think there's a valid exception to be made if you are, say, a group of pure mathematicians or theoretical physicists, who are already extremely familiar with that notation.

I can also see doing it if you're writing mathematic or physics library functions. Don't have any of it face the public, but if you can get the internals of your function to line up with the established scientific notation, that can make it easy for someone deep in the field to 'check your work' and make sure you've implemented it correctly.

If you want to show someone you've implemented an equation or algorithm, having your language be identical can be nice. But it's one of those things where if you have to ask, the answer is probably no.

(I also will use greek identifiers when loving around on project euler on my own time. drat it, it's ϕ(n), not totient(n)!)

Eela6 fucked around with this message at 20:17 on Feb 1, 2017

No Safe Word
Feb 26, 2005

Hammerite posted:

It's a bad convention. It's unnecessarily cryptic, and fails to be self-explanatory. On the other hand if you name a variable "unused", that's self-explanatory.

code:
>>> year, month, day, _, _, _, _, _, _ = tuple(datetime.datetime.now().utctimetuple())
>>> year, month, day, unused, unused, unused, unused, unused, unused = tuple(datetime.datetime.now().utctimetuple())
Contrived but you pick which one is better. You generally shouldn't have unused stuff at all, and the rare cases where it is used is because you need a terse way to skip it. A single, obviously-intended-to-not-be-used character, does that job more effectively than a word. And it's already internationalized to boot!

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

No Safe Word posted:

code:
>>> year, month, day, _, _, _, _, _, _ = tuple(datetime.datetime.now().utctimetuple())
>>> year, month, day, unused, unused, unused, unused, unused, unused = tuple(datetime.datetime.now().utctimetuple())
Contrived but you pick which one is better. You generally shouldn't have unused stuff at all, and the rare cases where it is used is because you need a terse way to skip it. A single, obviously-intended-to-not-be-used character, does that job more effectively than a word. And it's already internationalized to boot!

I'd use the second one but I'd put it in a helper function so I wouldn't have to look at it. I'm a bit baffled by the "internationalized" comment; are you saying you translate your program code into other languages?

Nippashish
Nov 2, 2005

Let me see you dance!

Hammerite posted:

It's a bad convention. It's unnecessarily cryptic, and fails to be self-explanatory. On the other hand if you name a variable "unused", that's self-explanatory.

I propose we rename 'zip' to 'as_tuples_by_index' because 'zip' is unnecessarily cryptic, and fails to be self-explanatory unless you already know what it's supposed to mean.

a witch
Jan 12, 2017

If someone was typing "unused" instead of "_" I would immediately assume they were inexperienced with Python.

Practicality beats purity and all that.

Eela6
May 25, 2007
Shredded Hen
IMO, the best way to do it is like this:
Python code:
year, month, day, *_ = tuple(datetime.datetime.now().utctimetuple())
Which I think communicates the intent the best.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Nippashish posted:

I propose we rename 'zip' to 'as_tuples_by_index' because 'zip' is unnecessarily cryptic, and fails to be self-explanatory unless you already know what it's supposed to mean.

There is some merit to this argument (unironically), but "zip" is at least a word. It doesn't do a great job of explaining what the function does though, you're right. It has always seemed to me that "transpose" or some variation on it would be a better name for zip.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I'd passive-aggresively go and change every variable called "unused" to "_" because goddamn it's a super common convention and whether or not you realize it or not conventions matter...a lot!

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Join usssss

QuarkJets
Sep 8, 2008

Nippashish posted:

I propose we rename 'zip' to 'as_tuples_by_index' because 'zip' is unnecessarily cryptic, and fails to be self-explanatory unless you already know what it's supposed to mean.

I think 'zip' is actually pretty simple to understand if you've ever seen or used a zipper before

Nippashish
Nov 2, 2005

Let me see you dance!

QuarkJets posted:

I think 'zip' is actually pretty simple to understand if you've ever seen or used a zipper before

zip is intuitive if you know what it means, but if you'd never encountered it before you might not know what it did just from the name. My argument is that _ is the same.

Eela6
May 25, 2007
Shredded Hen

Nippashish posted:

zip is intuitive if you know what it means, but if you'd never encountered it before you might not know what it did just from the name. My argument is that _ is the same.

Absolutely. But both of them are powerful constructs that allow a programmer to convey their ideas more clearly to a fluent audience.

Whenever you're writing anything, you have to consider your target audience. If I were teaching an intro to programming class, I would avoid things like closures or "yield from" or magic methods. I might even avoid zip and stick with enumerate and c-style indexing.

But if I'm writing for python professionals, I use all of these conventions. Conventions allow us to express high-level ideas quickly and conveniently.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Like...what do you gain by using the word "unused"?

As far as I can tell the only thing you gain is readability by super-new programmers at the expense of making it harder to read for everyone else.

QuarkJets
Sep 8, 2008

Nippashish posted:

zip is intuitive if you know what it means, but if you'd never encountered it before you might not know what it did just from the name. My argument is that _ is the same.

But only one of those references something in the real world. The other is an arbitrarily-decided convention

I'm not saying that _ is a bad convention, but your analogy is really weak

QuarkJets
Sep 8, 2008

It's also worth mentioning that _ is usually used as an alias for the gettext function so you're also committing the sin of shadowing a function with a variable, but _ is a terrible function name. Still, it's easy to get into trouble if you're working on code that uses this badly-named alias

Pylint regards any local variable with an underscore in front of it (such as _unused, _foo, _bar) as unused, which seems like it's the most Pythonic solution

Python code:
child, parent_, pie_plate = return_pie_state(person)
By convention, this code says that the parent is unused, but if someone came along later and wanted to use the parent then they easily could without having to check the API. That is both advantageous and more in accordance with "explicit is better than implicit" (even if it's not totally in accordance, merely moreso than using _)

Note that _ isn't mentioned in PEP8 anywhere, I think it's the kind of convention that just kind of spread by word of mouth and is generally safe to ignore for those who want to ignore it

Dominoes
Sep 20, 2007

No Safe Word posted:

code:
>>> year, month, day, _, _, _, _, _, _ = tuple(datetime.datetime.now().utctimetuple())
>>> year, month, day, unused, unused, unused, unused, unused, unused = tuple(datetime.datetime.now().utctimetuple())
Contrived but you pick which one is better. You generally shouldn't have unused stuff at all, and the rare cases where it is used is because you need a terse way to skip it. A single, obviously-intended-to-not-be-used character, does that job more effectively than a word. And it's already internationalized to boot!

This is the way to handle it:
code:
>>> year, month, day, *rest = tuple(datetime.datetime.now().utctimetuple())
*what Ela said.

No Safe Word
Feb 26, 2005

Dominoes posted:

This is the way to handle it:
code:
>>> year, month, day, *rest = tuple(datetime.datetime.now().utctimetuple())
*what Ela said.

You whippersnappers with your expanded tuple unpacking. *waves python 1.5.2 stick*

Eela6
May 25, 2007
Shredded Hen
As of yesterday, Anaconda released version 4.3 - which has Python 3.6 as a default! Now is a great time to start using Python 3.6.

If you're not sold on Python 3.6, here's another excellent youtube video by Raymond Hettinger https://www.youtube.com/watch?v=p33CVV29OG8 explaining the amazing new dictionary implementation.

Plus:

Python code:
>>>from math import pi
>>>print(f'pi = {pi}')
pi = 3.141592653589793

Eela6 fucked around with this message at 07:23 on Feb 2, 2017

Proteus Jones
Feb 28, 2013



Eela6 posted:

As of yesterday, Anaconda released version 4.3 - which has Python 3.6 as a default! Now is a great time to start using Python 3.6.

Yay!

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Thermopyle posted:

Like...what do you gain by using the word "unused"?

As far as I can tell the only thing you gain is readability by super-new programmers at the expense of making it harder to read for everyone else.

I've dragged this argument (in which I am clearly in the minority) out for too many posts already, but... I don't see that it does make it harder to read for anybody, irrespective of their level of experience.

  • Locked thread