8 Python Coding Tips - From The Google Python Style Guide

8 Python Coding Tips - From The Google Python Style Guide

ArjanCodes

2 года назад

152,504 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Victor Nikolov
Victor Nikolov - 01.10.2023 11:05

I love generators. They are memory cheap.

Ответить
Simon Howroyd
Simon Howroyd - 08.09.2022 22:04

"Having readable code is more important than having efficient code". I definitely don't agree with this for my embedded world. Efficient code is often required. However, that list comprehension is horrible, I agree, but could easily be made readable with a comment. One of the few good uses of a comment, to explain something not readable, that has been done for performance.

Ответить
Kevin Codes
Kevin Codes - 24.08.2022 20:04

Cool video. Nice to go over best practices to avoid spaghetti code 🍜🍝

Ответить
Muhammad Ali Merali
Muhammad Ali Merali - 09.08.2022 20:09

What kind of Python he is using? Never saw def foo(my_list: list....) -> int?

Ответить
Rahul Das
Rahul Das - 06.08.2022 07:50

Great , Thanks I was searching for this video

Ответить
wartem
wartem - 01.07.2022 14:30

If you absolutely need to, using a property is the Pythonic way to deal with it!

Ответить
Vijay Babaria
Vijay Babaria - 10.06.2022 06:41

@arjoncodes can’t stop watching your content. Truly amazing stuff. If you ever get chance, could you make videos on api development and good practices around that topic please - thanks

Ответить
Clive Barrett
Clive Barrett - 08.05.2022 11:46

I love being a mere mortal. By the way, thanks mate.

Ответить
Phil L
Phil L - 02.05.2022 07:03

There is no perfect way to code. Just dont be sloppy. Google isnt the king of the world, they provide a service, and the style guide is probably informed by their particular business

Ответить
Shreyas Subhedar
Shreyas Subhedar - 18.04.2022 06:57

very insightful video

Ответить
Shahbaz Manzoor
Shahbaz Manzoor - 13.04.2022 19:59

A1

Ответить
tylerl
tylerl - 09.04.2022 06:35

I've been writing Python code (among other languages) at Google for many years, and I definitely write better code since I started working there. Here are some ways that the way I write code changed:

* No code is temporary. Assume anything you check in will be used in production, because someone might decide to do so. They won't ask you.
* Code isn't useful if you don't have proper unit tests. You literally can't check it in without tests.
* Clear is better than fast. Clear is better than brief. Clever code and implicit assumptions are the first place you look to find the bugs.
* Your tests are your spec. Anyone is free to change your code; the only way to ensure they know what it is supposed to do is to test it properly.
* A given commit can EITHER refactor (in which case your tests do not change) OR change functionality (in which case your tests demonstrate the change in expectations). Never do both at the same time. You eliminate most mistakes this way.

It's not in the public style guide (because it conflicts with pep8), but 2 spaces instead of 4 is used across all languages. It sounds dumb but once you go 2-space you will never be able to go back. Cannot unsee. 4-space feels like the old 8-space standard. Cannot unsee... Cannot go back. My eyes are ruined forever. 2-space it must be forever...

Ответить
Miguel Vásquez
Miguel Vásquez - 02.04.2022 20:04

I have a doubt with the imports, if it is true that sometimes it is valid to know where a class or function comes from, there are times when it seems obvious to me, for example, I will not import "from django.core import exceptions" then use "except exceptions.ObjectDoesNotExist"... or, "from modules.mymodule import serializers" to use "serializer_class = serializers.MySerializer" in my ViewSet. Sometimes I find it unnecessary to do this.

Ответить
Now you know
Now you know - 30.03.2022 10:43

My python mentor.
Why didn't i find you ever since.
Keep up the great content.
I am really learning alot.

Ответить
Stephen S
Stephen S - 10.02.2022 04:52

Great video, it would be nice to hear your thoughts on Google's doc block style guide, I've noticed in your videos you use the more simple one-line explanation docblock (which may be for keeping the lesson easier to understand). I come from a PHP / Java background so when picking which doc block style guide to use, Google's felt familiar to me compared to phpdoc (summary, params/args, returns, throws/raises).

An interviewer raised a question to me that doc blocks may be redundant if you use strict types, type hinting and DAMP naming styles. I get his point but it's hard to undo years of habit-formed documentation because it just doesn't look right anymore (also can't automatically generate an api doc or provide extra info in IDE's when hovering methods etc). Be good to know where you stand on this :)

Ответить
Daniel Isidro Custodio Duran
Daniel Isidro Custodio Duran - 23.01.2022 20:37

Doesn't it increase the memory usage when import an entire module and just one function is used? 🙃

Ответить
Joao Santos
Joao Santos - 21.01.2022 13:46

the list default arguments have been useful to me when writting recursive code though. or was i doing something very silly? doesn't happen often, but tree traversal is one case.

Ответить
Nick Vledder
Nick Vledder - 19.01.2022 17:13

Why is Python so terribly slow? ... Probably I got too much used of the speed of PHP.

Ответить
CodeRider
CodeRider - 19.01.2022 10:40

The good MS / Java devs transition to python well but others tend to struggle so this provides a credible documented way to approach things.

Ответить
hyqhyp
hyqhyp - 18.01.2022 10:33

Whats a "physical computation?"

Ответить
MyChannel
MyChannel - 18.01.2022 06:46

really appreciate your effort. wondering if you could make more video about this topic

Ответить
/u/forgotmy_username
/u/forgotmy_username - 14.01.2022 09:55

We did this on my team with Google's JS style guide.

Ответить
Jeronimo
Jeronimo - 14.01.2022 00:49

Just keep in mind that the order of "for" loops and their variables stays the same when going from loops in a comprehension to 'normal' nested loops and vice versa. So "[bla for x in range(5) for y in range(5)]" translates to "for x in range(5): for y in range(5): bla". Since I had learned this easy rule of thumb from someone on stackoverflow, I don't find these list comprehensions hard to read at all. At least I'd doubt the traditional loop code looked so much better.

Ответить
AlpNix Tech
AlpNix Tech - 13.01.2022 19:00

Thanks for all these tips! As an extra tip, I would suggest to write short and clear functions that can be used for creating bigger functions. Basically this way, you won't be repeating any code you write and each of your functions will have not much code, so they will be easy to read.

Ответить
john anih
john anih - 13.01.2022 17:22

Great stuff!

Ответить
Golam Muhaimeen
Golam Muhaimeen - 13.01.2022 05:47

really interesting share Arjan! Great video man!

Ответить
Andrew Glick
Andrew Glick - 12.01.2022 22:08

About the minimizing the usage of try/except clauses when you don't care about the error-- If you plan for another part of your code to deal with an error, I'm of the opinion that explicitly reraising the exception (with `except: raise`) is better than relying on the built-in behavior for Python to reraise an exception. This way, you are clearly acknowledging that a part of your code can raise an error and that it is handled elsewhere.

If I'm looking at some code that calls some function which can raise an error and isn't inside a try/except block, I have no way of knowing if that error is caught elsewhere in the code or is just assumed to not raise an error.

Ответить
nuynobi
nuynobi - 12.01.2022 03:15

The thing about mutable default values in functions is actually useful for memoization. The parameter's name should indicate its purpose (eg "memo"). It can start off empty or contain a base case or two. Thoughts?

Ответить
Diogenes Laërtius
Diogenes Laërtius - 11.01.2022 10:11

Fuck Google and their fucking style guide.

Ответить
João Ponte
João Ponte - 10.01.2022 12:39

I got caught in the nested loops inside list comprehensions, lol

Ответить
musicologyman
musicologyman - 09.01.2022 23:31

I’m of two minds regarding list comprehensions.

On the one hand, reading code that uses functions like map and filter for operations on collections can be easy to follow, if you’re stringing together operations, it starts looking ugly. (One solution, perhaps, is to use packages like toolz that provide functions for piping, currying, and composing functions—but how pythonic is that?)

On the other hand—and I’ll admit that my affinity for functional programming is really showing here—I don’t see that even a moderately complicated list comprehension is that confusing if formatted in a way that’s easy to understand. After all, list comprehension syntax resembles set notation; placing each for expression on its own line clarifies what’s going on quite a bit. I think that approach may make sense within a shop whose culture encourages thinking that way.

In either case, there comes a point where readability is compromised. In those cases I find it useful simply to write a finction that returns a generator for the collection (using yield): sure, it’s not as declarative (and I really prefer—non-pythonically?—declarative over imperative code), but it might be easier for others to maintain.

Ответить
Vivek Joshi
Vivek Joshi - 09.01.2022 22:13

Google teaches R in their Google Data Analytics Specialization and talk about Using Python. Python is the most preferred language and they taught entire data analysis and viz in R.

Ответить
TNothingFree
TNothingFree - 09.01.2022 16:26

Great video!

Would be cool to see some more "Insights" into the python interpreter because a lot of silly mistakes like the default variables and variable nesting are due to the nature of Python being a dynamic interpreted language.

Ответить
Felipe Alvarez
Felipe Alvarez - 09.01.2022 08:03

For getters and setters could you show examples when they can be used neatly with @property Decorator?

Ответить
Andrei R
Andrei R - 09.01.2022 00:35

My wife (a google employee) thought I've being watching a couples therapy video

Ответить
Tekay37
Tekay37 - 08.01.2022 21:38

I think the guide is very good for people who build a library or at least create code that other people use within their code.
IN our company we have code that runs on our customer's servers, so the program must never crash and must always give some response our customer can send us. This means that we have certain places where we actually have to just catch every Exception that might come, especially those we don't expect.

Ответить
Robert Brummayer
Robert Brummayer - 08.01.2022 12:35

The only thing where I disagree is that you rather do not put assertions in your code but in your unit tests. Let me point out that there is a different kind of assertions that does not belong into a unit test, e.g. assertions about intermediate results and loop invariants. Moreover, assertions in your code also function as an executable documentation what property is expected to hold at this location in you program. This can give you further insights about how the code works. Since this "documentation" is executed, it typically does not rot as pure text does in comments. Personally, I use assertions a lot and combine it with advanced testing techniques that use randomization. I was already often surprised that assertions failed in my code I never thought possible, thus finding wrong assumptions and design flaws.

Ответить
Robert Brummayer
Robert Brummayer - 08.01.2022 12:09

Great video, Arjan. Fully agree with points about properties and getters/setters.

Ответить
adjbutler
adjbutler - 08.01.2022 11:48

Thank you for giving away your golden thoughts...

Ответить
Harsh Raj
Harsh Raj - 08.01.2022 09:06

Background music is good. Can you share it's link as well ?
Btw, great content : )

Ответить