Dusty Phillips

  • DDaudalagidalıntı yaptı2 yıl önce
    Finally, we have the property declaration at the bottom. This is the magic. It creates a new attribute on the Color class called name, which now replaces the previous name attribute. It sets this attribute to be a property, which calls the two methods we just created whenever the property is accessed or changed.
  • DDaudalagidalıntı yaptı2 yıl önce
    Think of the property function as returning an object that proxies any requests to set or access the attribute value through the methods we have specified. The property keyword is like a constructor for such an object, and that object is set as the public facing member for the given attribute.
  • DDaudalagidalıntı yaptı2 yıl önce
    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.
  • DDaudalagidalıntı yaptı2 yıl önce
    It is advisable to create an abstract base class in this case to document what API the third-party plugins should provide.
  • DDaudalagidalıntı yaptı2 yıl önce
    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.
  • DDaudalagidalıntı yaptı2 yıl önce
    The trickiest aspects of these classes is going to be ensuring superclass methods get called in the inheritance hierarchy.
  • DDaudalagidalıntı yaptı2 yıl önce
    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?
  • DDaudalagidalıntı yaptı2 yıl önce
    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.
  • DDaudalagidalıntı yaptı2 yıl önce
    Errors are sometimes considered more dire than exceptions, but they are dealt with in exactly the same way.
  • DDaudalagidalıntı yaptı2 yıl önce
    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.
fb2epub
Dosyalarınızı sürükleyin ve bırakın (bir kerede en fazla 5 tane)