What is the proper level of indent for hanging indent with type hinting in python? -


what proper syntax hanging indent method multiple parameters , type hinting?

align under first parameter

def get_library_book(self,                      book_id: str,                      library_id: str                      )-> book: 

indent 1 level beneath

def get_library_book(     self,      book_id: str,      library_id: str ) -> book: 

pep8 supports indent 1 level beneath case, not specify if align under first parameter allowed. states:

when using hanging indent following should considered; there should no arguments on first line , further indentation should used distinguish continuation line.

pep8 has many ideas in it, wouldn't rely on decide kind of question whitespace. when studied pep8's recommendations on whitespace, found them inconsistent , contradictory.

instead, @ general principles apply programming languages, not python.

the column alignment shown in first example has many disadvantages, , don't use or allow in of projects.

some of disadvantages:

  • if change function name length different, must realign of parameters.
  • when realignment, source control diffs cluttered unnecessary whitespace changes.
  • as code updated , maintained, it's you'll miss of alignment when renaming variables, leading misaligned code.
  • you longer line lengths.
  • the alignment doesn't work in proportional font. (yes, developers prefer proportional fonts, , if avoid column alignment, code equally readable in monospaced or proportional fonts.)

it gets worse if use column alignment in more complex cases. consider example:

let mut rewrites = try_opt!(subexpr_list.iter()                                         .rev()                                         .map(|e| {                                             rewrite_chain_expr(e,                                                                total_span,                                                                context,                                                                max_width,                                                                indent)                                         })                                         .collect::<option<vec<_>>>()); 

this rust code servo browser, coding style mandates kind of column alignment. while isn't python code, same principles apply in python or language.

it should apparent in code sample how use of column alignment leads bad situation. if needed call function, or had longer variable name, inside nested rewrite_chain_expr call? you're out of room unless want very long lines.

compare above either of these versions use purely indentation-based style second python example:

let mut rewrites = try_opt!(     subexpr_list         .iter()         .rev()         .map( |e| {             rewrite_chain_expr( e, total_span, context, max_width, indent )         })         .collect::<option<vec<_>>>() ); 

or, if parameters rewrite_chain_expr longer or if wanted shorter lines:

let mut rewrites = try_opt!(     subexpr_list         .iter()         .rev()         .map( |e| {             rewrite_chain_expr(                 e,                 total_span,                 context,                 max_width,                 indent             )         })         .collect::<option<vec<_>>>() ); 

in contrast column-aligned style, pure indentation style has many advantages , no disadvantages @ all.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -