Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Google's Internal python Code Specification

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "sample analysis of Google's internal python code specifications", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of Google's internal python code specifications".

Google's internal python code specification

Familiar with python will generally strive to follow the pep8 specification, and some companies will develop internal code specifications. The purpose of big companies is not to talk about how you have to use the programming language, but to make everyone follow the same set of rules, save others the cost of reading the code, and facilitate collaboration and communication. For individuals, writing code every day just needs to be consistent and consistent, which is a big victory, and then add some practical rules to effectively reduce the possible introduction of bug when writing code.

Next, I intercepted some interesting points from Google's python code specification, hoping to get a deeper sense of their usefulness in the future.

1. Import modules and packages, not individual classes, functions, or variables.

This usually simplifies the import process and facilitates namespace management. But the disadvantage is also obvious, when the name is long, the code to call functions and classes will be very long, affecting readability.

# yesfrom sound.effects import echoecho.EchoFilter () # nofrom sound.effects.echo import EchoFilterEchoFilter () 2. Import from the root directory, without assuming any sys.path or using relative imports.

Assuming that the doctor.who path has been added to the sys.path by some means, it should also be imported from the beginning.

# yesfrom doctor.who import jodie # other than doctor.who is already in sys.path# noimport jodie3. Use exceptions with caution

The details you need to pay attention to when using exceptions are:

Priority and reasonable use of built-in exception classes. For example, if a positive number is required, an error caused by a negative number is passed, and the ValueError type is thrown.

Never use except to catch all exceptions, which can make some hidden bug difficult to find. You should use specific exception types to catch separately.

Do not use assert to indicate unexpected conditions, you should use raise.

Don't add too much logic to the try and except clauses; the larger the try block, the easier it is for unexpected exceptions to be triggered.

Try to use the correct built-in exception type:

Def division (a, b): if b = 0: raise ValueError ('b can not be zero')

Avoid catching exceptions globally and specify the exception type:

# yestry: 1 / 0 "abc" [100] except ZeroDivisionError:... except IndexError:... # notry: 1 / 0 "abc" [100] except:... 4. Do not use a variable type as the default value of a function. If you modify this variable, the default value will change accordingly. # yesdef foo (a, b=None): if b is None: B = [] def foo (a, b: Sequence = ()): # nodef foo (a, b = []): def foo (a, b=time.time ()): def foo (a, b = {}): 5. Note the implicit Boolean value of the conditional expression

For sequences (strings, lists, tuples), note that the empty sequence is False. When determining whether the sequence is empty, use implicit if not seq instead of if len (seq) = = 0.

To determine whether the number is 0, use number = = 0 instead of using if not number. Because number may set the default value to None.

Determine whether to use x is None for None instead of not x.

# yes.if not users: # sequenceif number = = 0:if I% 10 = = 0:def f (x=None): if x is None: # noif len (users) = = 0:if number is not None and not number:if not I% 10:def f (x=None): X = x or [] 6. Use decorators with caution

The decorator can do anything on the arguments or return values of the function, which can lead to amazing hidden behavior. Moreover, the decorator executes on import, and it is difficult to catch errors from the decorator code and handle them. When using the decorator, be sure to write a unit test and explain its function and usage. The decorator itself should not rely on any files, socket, database connections. Avoid using the @ staticmedthod decorator; in most cases, encapsulating methods into module-level functions can achieve the same effect.

7. Type declaration is recommended, and the benefits of type declaration are obvious:

Use type declarations to improve the readability of your code.

You can also use the type checking tool to find problems early.

After using the type declaration, there is no need to specify the parameter type in doc string.

Code hints are made in the editor based on the type.

However, in practice, type declarations are often difficult to maintain. When the code is updated, be sure to update the type declaration. Outdated type declarations can be misleading to the reader. The type declaration cost of python is higher than the learning cost.

# yesname: str = 'yuz'def func (a: int)-> List [int]: the above is all the contents of the article "sample Analysis of Google's Internal python Code Specification". Thank you for reading! I believe you all have a certain understanding, hope to share the content is helpful to you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report