Skip to main content
Code Review

Return to Answer

Integer division; added 74 characters in body
Source Link
AJNeufeld
  • 35.2k
  • 5
  • 41
  • 103

__members

Adding a double underscore prefix (with at most one trailing underscore) to class/instance members has special meaning. It causes the interpreter to use name-mangling to avoid name clashes in subclasses.

If PreprocessNumber is not subclassed, there is no need to invoke name mangling.

→ Simply use a single leading underscore for private/protected members, not a double underscore.

Docstrings

Listing all the methods of a class in the docstring of the class is redundant. Executing help(PreprocessNumber) will generate help text from the class’s docstring and the docstrings of all public members in the class. If the class docstring includes information on the public members, it then appears twice in the output!

Providing docstrings for non-public members of a class is usually not useful, as that documentation will not normally be emitted by the help system.

Providing docstrings for name-mangled members is hilarious, as the members are not accessible unless one also knows the mangled name.

→ Provide docstrings for public classes and methods only; comments are generally sufficient for private/protected members.

Class Constants

Instead of creating an extra member on each and every instance of a class, create constants directly on the class:

class PreprocessNumber:
 """..."""
 NOT_BLACK_THRESHOLD = 20
 def __init__(self, image):
 ...

The class attribute can still be accessed using self.NOT_BLACK_THRESHOLD in member functions.

Note: Assigning to self.NOT_BLACK_THRESHOLD will create a instance member with the new value; it will not change the class’s attribute value. Assigning to PreprocessNumber.NOT_BLACK_THRESHOLD will change the class attribute "constant" seen by all instances.

Integer Division

Python has an integer division operator (//), so instead of this:

center_row = int(self.dimension[0] / 2)

you can write:

center_row = self.dimension[0] // 2

avoiding the conversions to floating point and then back to an integer.

__members

Adding a double underscore prefix (with at most one trailing underscore) to class/instance members has special meaning. It causes the interpreter to use name-mangling to avoid name clashes in subclasses.

If PreprocessNumber is not subclassed, there is no need to invoke name mangling.

→ Simply use a single leading underscore for private/protected members, not a double underscore.

Docstrings

Listing all the methods of a class in the docstring of the class is redundant. Executing help(PreprocessNumber) will generate help text from the class’s docstring and the docstrings of all public members in the class. If the class docstring includes information on the public members, it then appears twice in the output!

Providing docstrings for non-public members of a class is usually not useful, as that documentation will not normally be emitted by the help system.

Providing docstrings for name-mangled members is hilarious, as the members are not accessible unless one also knows the mangled name.

→ Provide docstrings for public classes and methods only; comments are generally sufficient for private/protected members.

Class Constants

Instead of creating an extra member on each and every instance of a class, create constants directly on the class:

class PreprocessNumber:
 """..."""
 NOT_BLACK_THRESHOLD = 20
 def __init__(self, image):
 ...

The class attribute can still be accessed using self.NOT_BLACK_THRESHOLD in member functions.

Note: Assigning to self.NOT_BLACK_THRESHOLD will create a instance member with the new value; it will not change the class’s attribute value. Assigning to PreprocessNumber.NOT_BLACK_THRESHOLD will change the class attribute "constant" seen by all instances.

__members

Adding a double underscore prefix (with at most one trailing underscore) to class/instance members has special meaning. It causes the interpreter to use name-mangling to avoid name clashes in subclasses.

If PreprocessNumber is not subclassed, there is no need to invoke name mangling.

→ Simply use a single leading underscore for private/protected members, not a double underscore.

Docstrings

Listing all the methods of a class in the docstring of the class is redundant. Executing help(PreprocessNumber) will generate help text from the class’s docstring and the docstrings of all public members in the class. If the class docstring includes information on the public members, it then appears twice in the output!

Providing docstrings for non-public members of a class is usually not useful, as that documentation will not normally be emitted by the help system.

Providing docstrings for name-mangled members is hilarious, as the members are not accessible unless one also knows the mangled name.

→ Provide docstrings for public classes and methods only; comments are generally sufficient for private/protected members.

Class Constants

Instead of creating an extra member on each and every instance of a class, create constants directly on the class:

class PreprocessNumber:
 """..."""
 NOT_BLACK_THRESHOLD = 20
 def __init__(self, image):
 ...

The class attribute can still be accessed using self.NOT_BLACK_THRESHOLD in member functions.

Note: Assigning to self.NOT_BLACK_THRESHOLD will create a instance member with the new value; it will not change the class’s attribute value. Assigning to PreprocessNumber.NOT_BLACK_THRESHOLD will change the class attribute "constant" seen by all instances.

Integer Division

Python has an integer division operator (//), so instead of this:

center_row = int(self.dimension[0] / 2)

you can write:

center_row = self.dimension[0] // 2

avoiding the conversions to floating point and then back to an integer.

Class "constants"; added 7 characters in body
Source Link
AJNeufeld
  • 35.2k
  • 5
  • 41
  • 103

__members

Adding a double underscore prefix (with at most one trailing underscore) to class/instance members has special meaning. It causes the interpreter to use name-mangling to avoid name clashes in subclasses.

If PreprocessNumber is not subclassed, there is no need to invoke name mangling.

→ Simply use a single leading underscore for private/protected members, not a double underscore.

Docstrings

Listing all the methods of a class in the docstring of the class is redundant. Executing help(PreprocessNumber) will generate help text from the class’s docstring and the docstrings of all public members in the class. If the class docstring includes information on the public members, it then appears twice in the output!

Providing docstrings for non-public members of a class is usually not useful, as that documentation will not normally be emitted by the help system.

Providing docstrings for name-mangled members is hilarious, as the members are not accessible unless one also knows the mangled name.

→ Provide docstrings for public classes and methods only; comments are generally sufficient for private/protected members.

Class Constants

Instead of creating an extra member on each and every instance of a class, create constants directly on the class:

class PreprocessNumber:
 """..."""
 NOT_BLACK_THRESHOLD = 20
 def __init__(self, image):
 ...

The class attribute can still be accessed using self.NOT_BLACK_THRESHOLD in member functions.

Note: Assigning to self.NOT_BLACK_THRESHOLD will create a instance member with the new value; it will not change the class’s attribute value. Assigning to PreprocessNumber.NOT_BLACK_THRESHOLD will change the class attribute "constant" seen by all instances.

__members

Adding a double underscore (with at most one trailing underscore) to class/instance members has special meaning. It causes the interpreter to use name-mangling to avoid name clashes in subclasses.

If PreprocessNumber is not subclassed, there is no need to invoke name mangling.

→ Simply use a single leading underscore for private/protected members, not a double underscore.

Docstrings

Listing all the methods of a class in the docstring of the class is redundant. Executing help(PreprocessNumber) will generate help text from the class’s docstring and the docstrings of all public members in the class. If the class docstring includes information on the public members, it then appears twice in the output!

Providing docstrings for non-public members of a class is usually not useful, as that documentation will not normally be emitted by the help system.

Providing docstrings for name-mangled members is hilarious, as the members are not accessible unless one also knows the mangled name.

→ Provide docstrings for public classes and methods only; comments are generally sufficient for private/protected members.

__members

Adding a double underscore prefix (with at most one trailing underscore) to class/instance members has special meaning. It causes the interpreter to use name-mangling to avoid name clashes in subclasses.

If PreprocessNumber is not subclassed, there is no need to invoke name mangling.

→ Simply use a single leading underscore for private/protected members, not a double underscore.

Docstrings

Listing all the methods of a class in the docstring of the class is redundant. Executing help(PreprocessNumber) will generate help text from the class’s docstring and the docstrings of all public members in the class. If the class docstring includes information on the public members, it then appears twice in the output!

Providing docstrings for non-public members of a class is usually not useful, as that documentation will not normally be emitted by the help system.

Providing docstrings for name-mangled members is hilarious, as the members are not accessible unless one also knows the mangled name.

→ Provide docstrings for public classes and methods only; comments are generally sufficient for private/protected members.

Class Constants

Instead of creating an extra member on each and every instance of a class, create constants directly on the class:

class PreprocessNumber:
 """..."""
 NOT_BLACK_THRESHOLD = 20
 def __init__(self, image):
 ...

The class attribute can still be accessed using self.NOT_BLACK_THRESHOLD in member functions.

Note: Assigning to self.NOT_BLACK_THRESHOLD will create a instance member with the new value; it will not change the class’s attribute value. Assigning to PreprocessNumber.NOT_BLACK_THRESHOLD will change the class attribute "constant" seen by all instances.

Source Link
AJNeufeld
  • 35.2k
  • 5
  • 41
  • 103

__members

Adding a double underscore (with at most one trailing underscore) to class/instance members has special meaning. It causes the interpreter to use name-mangling to avoid name clashes in subclasses.

If PreprocessNumber is not subclassed, there is no need to invoke name mangling.

→ Simply use a single leading underscore for private/protected members, not a double underscore.

Docstrings

Listing all the methods of a class in the docstring of the class is redundant. Executing help(PreprocessNumber) will generate help text from the class’s docstring and the docstrings of all public members in the class. If the class docstring includes information on the public members, it then appears twice in the output!

Providing docstrings for non-public members of a class is usually not useful, as that documentation will not normally be emitted by the help system.

Providing docstrings for name-mangled members is hilarious, as the members are not accessible unless one also knows the mangled name.

→ Provide docstrings for public classes and methods only; comments are generally sufficient for private/protected members.

lang-py

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