bookmate game

Dusty Phillips

  • DDaudalagidцитує2 роки тому
    Abstract base classes, or ABCs, define a set of methods and properties that a class must implement in order to be considered a duck-type instance of that class.
  • DDaudalagidцитує2 роки тому
    It is advisable to create an abstract base class in this case to document what API the third-party plugins should provide.
  • DDaudalagidцитує2 роки тому
    For now, just know that by marking a method or property as being abstract, you are stating that any subclass of this class must implement that method or supply that property in order to be considered a proper member of the class.
  • DDaudalagidцитує2 роки тому
    The trickiest aspects of these classes is going to be ensuring superclass methods get called in the inheritance hierarchy.
  • DDaudalagidцитує2 роки тому
    If we were to inherit from a class that provided validation functionality, the functionality would also have to be provided as a static method that did not access any instance variables on the class. If it doesn't access any instance variables, what's the point of making it a class at all? Why don't we just make this validation functionality a module-level function that accepts an input string and a list of valid answers, and leave it at that?
  • DDaudalagidцитує2 роки тому
    These two classes don't have a superclass (other than object), but we still call super().__init__ because they are going to be combined with the other classes, and we don't know what order the super calls will be made in.
  • DDaudalagidцитує2 роки тому
    Errors are sometimes considered more dire than exceptions, but they are dealt with in exactly the same way.
  • DDaudalagidцитує2 роки тому
    The problem with the preceding code is that it will catch any type of exception. What if we were writing some code that could raise both a TypeError and a ZeroDivisionError? We might want to catch the ZeroDivisionError, but let the TypeError propagate to the console.
  • DDaudalagidцитує2 роки тому
    The last line reraises the ValueError, so after outputting No, No, not 13!, it will raise the exception again; we'll still get the original stack trace on the console.
  • DDaudalagidцитує2 роки тому
    Note how the print statement in the finally clause is executed no matter what happens. This is extremely useful when we need to perform certain tasks after our code has finished running (even if an exception has occurred).
fb2epub
Перетягніть файли сюди, не більш ніж 5 за один раз