module 3

Cards (94)

  • In the procedural approach, it's possible to distinguish two different and completely separate worlds: the world of data, and the world of code. The world of data is populated with variables of different kinds, while the world of code is inhabited by code grouped into modules and functions.
  • The object approach suggests a completely different way of thinking. The data and the code are enclosed together in the same world, divided into classes.
  • Every class is like a recipe which can be used when you want to create a useful object
  • Every object has a set of traits (they are called properties or attributes – we'll use both words synonymously) and is able to perform a set of activities (which are called methods).
  • Objects are incarnations of ideas expressed in classes, like a cheesecake on your plate is an incarnation of the idea expressed in a recipe printed in an old cookbook.
  • Note: the hierarchy grows from top to bottom, like tree roots, not branches. The most general, and the widest, class is always at the top (the superclass) while its descendants are located below (the subclasses).
  • Note the direction of the arrows – they always point to the superclass. The top-level class is an exception – it doesn't have its own superclass.
  • A class (among other definitions) is a set of objects. An object is a being belonging to a class.
  • An object is an incarnation of the requirements, traits, and qualities assigned to a specific class. 
  • Each subclass is more specialized (or more concrete) than its superclass. Conversely, each superclass is more general (more abstract) than any of its subclasses.
  • inheritance
    Any object bound to a specific level of a class hierarchy inherits all the traits (as well as the requirements and qualities) defined inside any of the superclasses.
  • The object programming convention assumes that every existing object may be equipped with three groups of attributes:
    • name
    • set of individual properties
    • set of abilities to perform specific activities
  • Object programming is the art of defining and expanding classes. A class is a model of a very specific part of reality, reflecting properties and activities found in the real world.
  • The act of creating an object of the selected class is also called an instantiation (as the object becomes an instance of the class).
  •  Objects are equipped with:
    • a name which identifies them and allows us to distinguish between them;
    • a set of properties (the set can be empty)
    • a set of methods (can be empty, too)
  • A stack is a structure developed to store data in a very specific way. 
  • The alternative name for a stack (but only in IT terminology) is LIFO.
    It's an abbreviation for a very clear description of the stack's behavior: Last In – First Out. The coin that came last onto the stack will leave first.
  • A stack is an object with two elementary operations, conventionally named push (when a new element is put on the top) and pop (when an existing element is taken away from the top).
    • the ability to hide (protect) selected values against unauthorized access is called encapsulation; the encapsulated values can be neither accessed nor modified if you want to use them exclusively;
    • the ability to enrich the stack with new functions comes from inheritance; you can create a new class (a subclass) which inherits all the existing traits from the superclass, and adds some new ones.
  • Such a function is called a constructor, as its general purpose is to construct a new object. The constructor should know everything about the object's structure, and must perform all the needed initializations.
    • the constructor's name is always __init__;
    • it has to have at least one parameter (we'll discuss this later); the parameter is used to represent the newly created object – you can use the parameter to manipulate the object, and to enrich it with the needed properties; you'll make use of this soon;
    • note: the obligatory parameter is usually named self – it's only a convention, but you should follow it – it simplifies the process of reading and understanding your code.
  • When any class component has a name starting with two underscores (__), it becomes private – this means that it can be accessed only from within the class.
    You cannot see it from the outside world. This is how Python implements the encapsulation concept.
  • note: invoking any method (including constructors) from outside the class never requires you to put the self argument at the argument's list – invoking a method from within the class demands explicit usage of the self argument, and it has to be put first on the list.
  • Implementing the stack in a procedural model raises several problems which can be solved by the techniques offered by OOP (Object Oriented Programming).
  • A stack is an object designed to store data using the LIFO model. The stack usually performs at least two operations, named push() and pop().
  • A class method is actually a function declared inside the class and able to access all the class's components.
  • The part of the Python class responsible for creating new objects is called the constructor, and it's implemented as a method of the name __init__.
  •  If we want to hide any of a class's components from the outside world, we should start its name with __. Such components are called private.
  • Python objects, when created, are gifted with a small set of predefined properties and methods. Each object has got them, whether you want them or not. One of them is a variable named __dict__ (it's a dictionary).
  • A class variable is a property which exists in just one copy and is stored outside any object.
  • Python provides a function which is able to safely check if any object/class contains a specified property. The function is named hasattr, and expects two arguments to be passed to it
  • he function is named hasattr, and expects two arguments to be passed to it:
    • the class or the object being checked;
    • the name of the property whose existence has to be reported (note: it has to be a string containing the attribute name, not the name alone)
    The function returns True or False.
  • Don't forget that the hasattr() function can operate on classes, too. You can use it to find out if a class variable is available
  •  An instance variable is a property whose existence depends on the creation of an object. Every object can have a different set of instance variables.
  •  An instance variable can be private when its name starts with __, but don't forget that such a property is still accessible from outside the class using a mangled name constructed as _ClassName__PrivatePropertyName.
  • class variable is a property which exists in exactly one copy, and doesn't need any created object to be accessible. Such variables are not shown as __dict__ content.
  • All a class's class variables are stored inside a dedicated dictionary named __dict__, contained in every class separately.
  •  A function named hasattr() can be used to determine if any object/class contains a specified property.
  • method is a function embedded inside a class.