How Do I Keep Python Code Under 80 Chars Without Making It Ugly?

后端 未结 7 497
醉梦人生
醉梦人生 2021-01-31 14:27

This is a question that keeps recurring in all of my programming, python and otherwise. I really like to keep my code under 80 chars if at all possible/not horribly ugly. In a l

7条回答
  •  心在旅途
    2021-01-31 14:50

    I find myself using more and more intermediate variables, that not only help stay within 80 characters, but make the code more readable by giving things descriptive names like:

    old_name = 'reallylonguglypath/to/current/file.foo'
    new_name = 'evenmoreuglylong/to/new/desination/for/file.foo' 
    os.rename(old_name, new_name)
    

    rather than:

    os.rename("reallylonguglypath/to/current/file.foo",
                  "evenmoreuglylong/to/new/desination/for/file.foo")
    

    You can do this with long module and class names too

    method = SomeLongClassName.SomeLongMethodName
    self.SomeLongLongName = method(some_obj, self.user1, self.user2)
    

提交回复
热议问题