Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This is mostly a review of the style.

Python has an official style-guide, PEP8.

It recommends using lower_case for variable and function names and PascalCase for class names.

It also recommends using whitespace in the following way:

  • After function and class definitions, two blank lines.

  • Between class methods, one blank line.

  • To separate code blocks within a function/method, one blank line.

  • operators should be surrounded by whitespace (so idx += 1), except when used as a keyword in a function-call (so f(a=1, b=2)). Commas are also followed by a space.

Python has comments, they start with a #. PEP8 recommends using two spaces after code (before the #) and one space after the #.

Examples:

# Normal, full line comment
some_code.action() # Does some cool stuff

Python also has docstrings, which serve to document code. The whole module, classes and functions/methods can take docstrings. They are described in PEP257.

They should look like this:

class A(object):
 """A normal class"""
 def __init__(self, *args):
 """
 The constructor
 Does some fancy stuff with `args`
 """
 self.args = args

As you can see, you can use multi-line strings. The indentation of the first line will be removed from the string.

You can access these strings e.g. by looking at the dunder __doc__ attribute (so A.__doc__ == "A normal class") or by using help(A.__init__) in an interactive shell. Also, a lot of documentation tools will pick up on this.

So, all your strings within the methods should be comments and start with #. All your strings directly above methods should be docstrings and be inside the methods (as the first line).

In addition to these comments, you should also use the if __name__ == "__main__": if __name__ == "__main__": guard, which allows importing your code from other scripts.

This is mostly a review of the style.

Python has an official style-guide, PEP8.

It recommends using lower_case for variable and function names and PascalCase for class names.

It also recommends using whitespace in the following way:

  • After function and class definitions, two blank lines.

  • Between class methods, one blank line.

  • To separate code blocks within a function/method, one blank line.

  • operators should be surrounded by whitespace (so idx += 1), except when used as a keyword in a function-call (so f(a=1, b=2)). Commas are also followed by a space.

Python has comments, they start with a #. PEP8 recommends using two spaces after code (before the #) and one space after the #.

Examples:

# Normal, full line comment
some_code.action() # Does some cool stuff

Python also has docstrings, which serve to document code. The whole module, classes and functions/methods can take docstrings. They are described in PEP257.

They should look like this:

class A(object):
 """A normal class"""
 def __init__(self, *args):
 """
 The constructor
 Does some fancy stuff with `args`
 """
 self.args = args

As you can see, you can use multi-line strings. The indentation of the first line will be removed from the string.

You can access these strings e.g. by looking at the dunder __doc__ attribute (so A.__doc__ == "A normal class") or by using help(A.__init__) in an interactive shell. Also, a lot of documentation tools will pick up on this.

So, all your strings within the methods should be comments and start with #. All your strings directly above methods should be docstrings and be inside the methods (as the first line).

In addition to these comments, you should also use the if __name__ == "__main__": guard, which allows importing your code from other scripts.

This is mostly a review of the style.

Python has an official style-guide, PEP8.

It recommends using lower_case for variable and function names and PascalCase for class names.

It also recommends using whitespace in the following way:

  • After function and class definitions, two blank lines.

  • Between class methods, one blank line.

  • To separate code blocks within a function/method, one blank line.

  • operators should be surrounded by whitespace (so idx += 1), except when used as a keyword in a function-call (so f(a=1, b=2)). Commas are also followed by a space.

Python has comments, they start with a #. PEP8 recommends using two spaces after code (before the #) and one space after the #.

Examples:

# Normal, full line comment
some_code.action() # Does some cool stuff

Python also has docstrings, which serve to document code. The whole module, classes and functions/methods can take docstrings. They are described in PEP257.

They should look like this:

class A(object):
 """A normal class"""
 def __init__(self, *args):
 """
 The constructor
 Does some fancy stuff with `args`
 """
 self.args = args

As you can see, you can use multi-line strings. The indentation of the first line will be removed from the string.

You can access these strings e.g. by looking at the dunder __doc__ attribute (so A.__doc__ == "A normal class") or by using help(A.__init__) in an interactive shell. Also, a lot of documentation tools will pick up on this.

So, all your strings within the methods should be comments and start with #. All your strings directly above methods should be docstrings and be inside the methods (as the first line).

In addition to these comments, you should also use the if __name__ == "__main__": guard, which allows importing your code from other scripts.

added 17 characters in body
Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134

This is mostly a review of the style.

Python has an official style-guide, PEP8.

It recommends using lower_case for variable and function names and PascalCase for class names.

It also recommends using whitespace in the following way:

  • After function and class definitions, two blank lines.

  • Between class methods, one blank line.

  • To separate code blocks within a function/method, one blank line.

  • operators should be surrounded by whitespace (so idx += 1), except when used as a keyword in a function-call (so f(a=1, b=2)). Commas are also followed by a space.

Python has comments, they start with a #. PEP8 recommends using two spaces after code (before the #) and one space after the #.

Examples:

# Normal, full line comment
some_code.action() # Does some cool stuff

Python also has docstrings, which serve to document code. The whole module, classes and functions/methods can take docstrings. They are described in PEP257.

They should look like this:

class A(object):
 """A normal class"""
 def __init__(self, *args):
 """
 The constructor
 Does some fancy stuff with `args`
 """
 self.args = args

As you can see, you can use multi-line strings. The indentation of the first line will be removed from the string.

You can access these strings e.g. by looking at the dunder __doc__ attribute (so A.__doc__ == "A normal class") or by using help(A.__init__) in an interactive shell. Also, a lot of documentation tools will pick up on this.

So, all your strings within the methods should be comments and start with #. All your strings directly above methods should be docstrings and be inside the methods (as the first line).

In addition to these comments, you should also use the if __name__ == "__main__": guard, which allows importing your code from other scripts.

This is mostly a review of the style.

Python has an official style-guide, PEP8.

It recommends using lower_case for variable and function names and PascalCase for class names.

It also recommends using whitespace in the following way:

  • After function and class definitions, two blank lines.

  • Between class methods, one blank line.

  • To separate code blocks within a function/method, one blank line.

  • operators should be surrounded by whitespace (so idx += 1), except when used as a keyword in a function-call (so f(a=1, b=2)). Commas are also followed by a space.

Python has comments, they start with a #. PEP8 recommends using two spaces after code (before the #) and one space after the #.

Examples:

# Normal, full line comment
some_code.action() # Does some cool stuff

Python also has docstrings, which serve to document code. The whole module, classes and functions/methods can take docstrings. They are described in PEP257.

They should look like this:

class A(object):
 """A normal class"""
 def __init__(self, *args):
 """
 The constructor
 Does some fancy stuff with `args`
 """

As you can see, you can use multi-line strings. The indentation of the first line will be removed from the string.

You can access these strings e.g. by looking at the dunder __doc__ attribute (so A.__doc__ == "A normal class") or by using help(A.__init__) in an interactive shell. Also, a lot of documentation tools will pick up on this.

So, all your strings within the methods should be comments and start with #. All your strings directly above methods should be docstrings and be inside the methods (as the first line).

In addition to these comments, you should also use the if __name__ == "__main__": guard, which allows importing your code from other scripts.

This is mostly a review of the style.

Python has an official style-guide, PEP8.

It recommends using lower_case for variable and function names and PascalCase for class names.

It also recommends using whitespace in the following way:

  • After function and class definitions, two blank lines.

  • Between class methods, one blank line.

  • To separate code blocks within a function/method, one blank line.

  • operators should be surrounded by whitespace (so idx += 1), except when used as a keyword in a function-call (so f(a=1, b=2)). Commas are also followed by a space.

Python has comments, they start with a #. PEP8 recommends using two spaces after code (before the #) and one space after the #.

Examples:

# Normal, full line comment
some_code.action() # Does some cool stuff

Python also has docstrings, which serve to document code. The whole module, classes and functions/methods can take docstrings. They are described in PEP257.

They should look like this:

class A(object):
 """A normal class"""
 def __init__(self, *args):
 """
 The constructor
 Does some fancy stuff with `args`
 """
 self.args = args

As you can see, you can use multi-line strings. The indentation of the first line will be removed from the string.

You can access these strings e.g. by looking at the dunder __doc__ attribute (so A.__doc__ == "A normal class") or by using help(A.__init__) in an interactive shell. Also, a lot of documentation tools will pick up on this.

So, all your strings within the methods should be comments and start with #. All your strings directly above methods should be docstrings and be inside the methods (as the first line).

In addition to these comments, you should also use the if __name__ == "__main__": guard, which allows importing your code from other scripts.

Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134

This is mostly a review of the style.

Python has an official style-guide, PEP8.

It recommends using lower_case for variable and function names and PascalCase for class names.

It also recommends using whitespace in the following way:

  • After function and class definitions, two blank lines.

  • Between class methods, one blank line.

  • To separate code blocks within a function/method, one blank line.

  • operators should be surrounded by whitespace (so idx += 1), except when used as a keyword in a function-call (so f(a=1, b=2)). Commas are also followed by a space.

Python has comments, they start with a #. PEP8 recommends using two spaces after code (before the #) and one space after the #.

Examples:

# Normal, full line comment
some_code.action() # Does some cool stuff

Python also has docstrings, which serve to document code. The whole module, classes and functions/methods can take docstrings. They are described in PEP257.

They should look like this:

class A(object):
 """A normal class"""
 def __init__(self, *args):
 """
 The constructor
 Does some fancy stuff with `args`
 """

As you can see, you can use multi-line strings. The indentation of the first line will be removed from the string.

You can access these strings e.g. by looking at the dunder __doc__ attribute (so A.__doc__ == "A normal class") or by using help(A.__init__) in an interactive shell. Also, a lot of documentation tools will pick up on this.

So, all your strings within the methods should be comments and start with #. All your strings directly above methods should be docstrings and be inside the methods (as the first line).

In addition to these comments, you should also use the if __name__ == "__main__": guard, which allows importing your code from other scripts.

lang-py

AltStyle によって変換されたページ (->オリジナル) /