Skip to main content
added 957 characters in body
Source Link
Ethan Bierlein
  • 15.8k
  • 4
  • 59
  • 146

Proper error catching

Never, ever have an empty except clause like the following:

try:
    ...
except:
    ...

With an empty except clause, any error that occurs in the try block will be caught by the except clause. This includes errors that aren't intended to be caught, like a SystemError, where something goes wrong internally.

The proper way to specify what error you want to catch would be to write your except clause like this:

except ExceptionType:
    ...

Properly opening files

The generally accepted method for opening files in Python is to use a contest manager, as seen below. If you use a context manager, the file is closed implicitly, and you can guarantee that the file is closed properly if the program unexpectedly exits:

with open("path/to/my/file.dat", "mode") as my_file:
    ...

OOP Design

It feels like you're not using object-oriented-programming correctly, as your __init__ methods seem to be doing More to come.way too much. As the name implies, the magic method __init__ should only be used for class initialization, and nothing else. Right now, you're treating the __init__ method like the "main method" of your class.

The best way that I can think of would be to do two things:

  1. Use a global state. I wouldn't highly recommend this, but if the program is small and trivial enough, then it's probably okay.
  2. Represent items like a Scissor, or a Rock with objects.

Nitpicks

  • Classes in Python 3.x are implicitly new-style, and don't need to explicitly inherit from object. This means they can be declared in a fashion like this:

      class MyClass:
          ...
    
  • Functions like stats() or final() should probably return a string, rather than explicitly printing one.

Proper error catching

Never, ever have an empty except clause like the following:

try:
    ...
except:
    ...

With an empty except clause, any error that occurs in the try block will be caught by the except clause. This includes errors that aren't intended to be caught, like a SystemError, where something goes wrong internally.

The proper way to specify what error you want to catch would be to write your except clause like this:

except ExceptionType:
    ...

Properly opening files

The generally accepted method for opening files in Python is to use a contest manager, as seen below. If you use a context manager, the file is closed implicitly, and you can guarantee that the file is closed properly if the program unexpectedly exits:

with open("path/to/my/file.dat", "mode") as my_file:
    ...

More to come.

Proper error catching

Never, ever have an empty except clause like the following:

try:
    ...
except:
    ...

With an empty except clause, any error that occurs in the try block will be caught by the except clause. This includes errors that aren't intended to be caught, like a SystemError, where something goes wrong internally.

The proper way to specify what error you want to catch would be to write your except clause like this:

except ExceptionType:
    ...

Properly opening files

The generally accepted method for opening files in Python is to use a contest manager, as seen below. If you use a context manager, the file is closed implicitly, and you can guarantee that the file is closed properly if the program unexpectedly exits:

with open("path/to/my/file.dat", "mode") as my_file:
    ...

OOP Design

It feels like you're not using object-oriented-programming correctly, as your __init__ methods seem to be doing way too much. As the name implies, the magic method __init__ should only be used for class initialization, and nothing else. Right now, you're treating the __init__ method like the "main method" of your class.

The best way that I can think of would be to do two things:

  1. Use a global state. I wouldn't highly recommend this, but if the program is small and trivial enough, then it's probably okay.
  2. Represent items like a Scissor, or a Rock with objects.

Nitpicks

  • Classes in Python 3.x are implicitly new-style, and don't need to explicitly inherit from object. This means they can be declared in a fashion like this:

      class MyClass:
          ...
    
  • Functions like stats() or final() should probably return a string, rather than explicitly printing one.

Source Link
Ethan Bierlein
  • 15.8k
  • 4
  • 59
  • 146

Proper error catching

Never, ever have an empty except clause like the following:

try:
    ...
except:
    ...

With an empty except clause, any error that occurs in the try block will be caught by the except clause. This includes errors that aren't intended to be caught, like a SystemError, where something goes wrong internally.

The proper way to specify what error you want to catch would be to write your except clause like this:

except ExceptionType:
    ...

Properly opening files

The generally accepted method for opening files in Python is to use a contest manager, as seen below. If you use a context manager, the file is closed implicitly, and you can guarantee that the file is closed properly if the program unexpectedly exits:

with open("path/to/my/file.dat", "mode") as my_file:
    ...

More to come.