python - pass keyword to mark end room -


i have noticed people used mark end of rooms pass keyworkd as

def f():     in range(10):         do_something(i)         pass     pass 

i don't understand why. there advantages?

i know pass keyword nothing , equivalent of { } in other languages. question more related convection. example maybe people have marking end of long room.

do think python's indention sensitive grammar harmful?

if answer "no", using pass mark end nothing clutter cleanness brought in python's indention sensitive grammar.

if answer "yes", using pass mark end workaround ambiguity brought in python's indention sensitive grammar.

in python, there no {} , end, semantic white spaces.

for example, consider following ruby code:

 def f     in 0...10         = * 2         print(i)     end end 

and equivalent python code:

 def f():     in range(10):         = * 2         print(i) 

with 1 wrong keystroke (tab):

 def f():     in range(10):         = * 2     print(i) 

the above code valid.

to avoid kind of mistakes, can use pass end in python:

 def f():     in range(10):         = * 2         print(i)         pass     pass 

with 1 wrong keystroke (tab):

 def f():     in range(10):         = * 2     print(i)         pass     pass 

python refuse work:

 indentationerror: unexpected indent 

however, python not catch unintended indentation pass.

suppose intend write:

 def g():     in range(10):         = * 2         pass     print(i)     pass 

with 1 wrong keystroke (tab):

 def g():     in range(10):         = * 2         pass         print(i)     pass 

python not refuse work.

but using pass still has 2 advantages:

  • it still provides visual hint.

    if think pass last clause of indented block, print(i) looks wired in above code.

  • a decent editor/ide indent correctly if typed pass.

for second pass, if f() top-level function, , code adheres pep8:

surround top-level function , class definitions 2 blank lines.

you may omit second pass.

but pep8 said:

extra blank lines may used (sparingly) separate groups of related functions.

thus prefer pass.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -