Summary

  • All types in Python have a class.

  • Classes define the structure and behavior of an object.

  • The class of an object is determined when the object is created and is almost always fixed for the lifetime of the object.

  • Classes are the key support for Object-Oriented Programming in Python.

  • Classes are defined using the class keyword followed by the class name, which is in CamelCase.

  • Instances of a class are created by calling the class as if it were a function.

  • Instance methods are functions defined inside the class which should accept an object instance called self as the first parameter.

  • Methods are called using the instance.method() syntax which is syntactic sugar for passing the instance as the formal self argument to the method.

  • An optional special initializer method called __init__() can be provided which is used to configure the self object at creation time.

  • The constructor calls the __init__() method if one is present.

  • The __init__() method is not the constructor. The object has been already constructed by the time the initializer is called. The initializer configures the newly created object before it it returned to the caller of the constructor.

  • Arguments passed to the constructor are forwarded to the initializer.

  • Instance attributes are brought into existence simply by assigning to them.

  • Attributes and methods which are implementation details are by convention prefixed with an underscore. There are no public, protected or private access modifiers in Python.

  • Access to implementation details from outside the class can be very useful during development, testing and debugging.

  • Class invariants should be established in the initializer. If the invariants can't be established raise exceptions to signal failure.

  • Methods can have docstrings, just like regular functions.

  • Classes can have docstrings.

  • Even within an object method calls must be qualified with self.

  • You can have as many classes and functions in a module as you wish. Related classes and global functions are usually grouped together this way.

  • Polymorphism in Python is achieved through duck typing where attributes and methods are only resolved at point of use - a behaviour called late-binding.

  • Polymorphism in Python does not require shared base classes or named interfaces.

  • Class inheritance in Python is primarily useful for sharing implementation rather than being necessary for polymorphism.

  • All methods are inherited, including special methods like the initialiser method.

Along the way we found that:

  • Strings support slicing, because they implement the sequence protocol.

  • Following the Law of Demeter can reduce coupling.

  • We can nest comprehensions.

  • It can sometimes be useful to discard the current item in a comprehension using a dummy reference, conventionally the underscore.

  • When dealing with one-based collections it’s often easier just to waste the zeroth list entry.

  • Don't feel compelled to use classes when a simple function will suffice. Functions are also objects.

  • Complex comprehensions or generator expressions can be split over multiple lines to aid readability.

  • Statements can be split over multiple lines using the backslash line continuation character. Use this feature sparingly and only when it improves readability.

  • Object-oriented design where one object tells another information can be more loosely coupled than those where one object queries another. "Tell! Don’t ask."

  1. In fact, it is possible to change the class of an object at runtime, although this is an advanced topic, and the technique is only rarely used.

  2. It's generally unhelpful to think about the destruction of objects in Python. Better to think of objects becoming unreachable.

  3. The formal arguments of a function are the arguments listed in the function definition.

  4. The actual arguments of a function are the arguments listed in a function call.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset