O GoConqr está de visual novo! Conte para nós o que você achou.

Mapa mental

Object Oriented Programming (OOP)

ajani walker Computer Science Inglês

Object Oriented Programming (OOP)

Conteúdo do mapa

Object Oriented Programming (OOP)

  1. Defining a New Python Class

    • Example Class Point
      • Definition
        • Below
          • A def statement defines a new function and gives the function a name; a class statement defines a new type and gives the type a name.
          • A class is defined by its attributes. The class attributes are the four methods of class Point.
          • To create a Point object, we would use the default constructor of the Point class. This is no different from using the list() or int() default constructors to create a list or integer object.
            • point = Point()
      • Methods
        • point.setx(xcoord) Sets the x coordinate of point to xcoord
        • point.sety(ycoord) Sets the y coordinate of point to ycoord
        • point.get() Returns the x and y coordinates of point as a tuple (x, y)
        • point.move(dx, dy) Changes the coordinates of point from the current (x, y) to (x+dx, y+dy)
        • We have learned that Python translates a method invocation like point.setx(3) to Point.setx(point, 3)
    • Namespace
      • a namespace is associated with every Python class, and the name of the namespace is the name of the class. The purpose of the namespace is to store the names of the class attributes.
      • every Python object has its own, separate namespace.
      • Instance Variables
        • Variables defined in the namespace of an object, such as variables x and y in the Point object a, are called instance variables. 
        • Class Attributes vs Instance variables
          • https://stackoverflow.com/questions/19753897/difference-between-class-variables-and-instance-variables
          • If every instance (object) with Class A needs its own separate copy of said variable, it should be an instance variable. If it should be shared across instances, then it should be a class attribute.
    • Documentation
      • In order to get usable documentation from the help() tool, it is important to document a new class properly. The class Point we defined has a docstring for the class and also one for every method
  2. Examples of User-Defined Classes

    • Constructors
      • With Input Arguments
        • __init__()
          • When added to a class, it will be automatically called by the Python interpreter whenever an object is created.
      • Default
        • Define them in __init__(self, arg1 =  def1, arg2 = def2)
        • Example w/ Point Class: def __init__(self, xcoord=0, ycoord=0): ...
        • I'm not sure how useful this is, but  The Python interpreter will assign the constructor arguments to the local variables xcoord and ycoord from left to right.
  3. Designing New Container Classes

    • There will always be a need to develop container classes tailored for specific applications.
    • Application Specific Terminology and Operations
      • Our previous applications were written using non-application specific terminology and operations. With classes, you can easily encapsulate the technical code language into easily read language.
    • Queue
      • FIFO
        • In a checkout queue, shoppers are served in a first-in first-out (FIFO) fashion
      • Example Class Queue
        • Definition
          • Above
            • Object Creation
              • fruit = Queue()
        • Methods
          • enqueue(item) Add item to the end of the queue
          • dequeue() Remove and return the element at the front of the queue
          • isEmpty() Returns True if the queue is empty, False otherwise
  4. Overloaded Operators

    • Consider the operator +. It can be used to add numbers: >>> 2 + 4 6 It can also be used to concatenate lists and strings: >>> [4, 5, 6] + [7] [4, 5, 6, 7] >>> 'strin' + 'g' 'string'
    • The + operator is said to be an overloaded operator. An overloaded operator is an operator that has been defined for multiple classes.
    • Problems
      • Object Evaluation
        • suppose you create a Point object: >>> point = Point(3,5) and then tried to evaluate it: >>> point <__main__.Point object at 0x15e5410> Not very user-friendly, is it?  Probably not the information we wanted to get when we evaluated point.
      • Built in classes vs our defined classes
        • len(fruit) Traceback (most recent call last): File "", line 1, in len(fruit) TypeError: object of type 'Queue' has no len()
        • The point we are making is this: The classes we have developed so far do not behave like built-in classes. For user-defined classes to be useful and easy to use, it is important to make them more more familiar (i.e., more like built-in classes).
    • Listed
      • str() vs repr()
        • class Representation: def __repr__(self): return 'canonical string representation' def __str__(self): return 'Pretty string representation.' Let’s test it: >>> rep = Representation() >>> rep canonical string representation >>> print(rep) Pretty string representation.
        • evaluations (repr) vs print function (str)
    • Contract between Constructor and __repr__()
      • What do we want the canonical string representation to look like? How about: >>> fruit Queue(['apple', 'banana', 'coconut']) Recall that when implementing the overloaded operator repr(), ideally we should satisfy the contract between it and the constructor. To satisfy it, we should be able to construct the queue as shown (the same way it is put out): >>> Queue(['apple', 'banana', 'coconut'])
      • Simply put, the canonical string representation going out must be the same as the constructor (__init__() ) going in. In the fruit example, we'd have to add an additional argument that accepts a list to the __init__ method
  5. Inheritance

    • Subclass: Inheriting all attributes of a class and extending it
      • This class definition specifies that class MyList is a subclass of the class list and thus supports all the methods that class list supports.  This is indicated in the first line: class MyList(list):
      • import random  class MyList(list):  'a subclass of list that implements method choice'  def choice(self):  'return item from list chosen uniformly at random'  return random.choice(self)
      • Namespace
    • Inheriting single values from a class
      • import random class MyList: def __init__(self, initial = []): self.lst = initial def __len__(self): return len(self.lst) def append(self, item): self.lst.append(self, item) # implementations of remaining "list" methods def choice(self): return random.choice(self.lst)
    • Class definition
      • 1 class <Class Name>: 2  class <Class Name> (<Super Class>): 3 class <Class Name> (<Super Class1>, <Super Class2>, ... ):
  6. User-Defined Exceptions

  7. In this chapter, we describe how to develop new Python classes. We also explain the benefits of the object-oriented programming (OOP) paradigm and discuss core OOP concepts that we will make use of in this chapter and in the chapters that follow. The idea that a class object can be manipulated by users through method invocations alone and without knowledge of the implementation of these methods is called abstraction. Abstraction facilitates software development because the programmer works with objects abstractly (i.e., through “abstract” method names rather than “concrete” code).

    • In order for abstraction to be beneficial, the “concrete” code and data associated with objects must be encapsulated (i.e., made “invisible” to the program using the object). Encapsulation is achieved thanks to the fact that (1) every class defines a namespace in which class attributes (variables and methods) live, and (2) every object has a namespace that inherits the class attributes and in which instance attributes live.

Imagens e arquivos do mapa

  • Object Oriented Programming (OOP)
  • Object Oriented Programming (OOP)
  • Object Oriented Programming (OOP)
  • Object Oriented Programming (OOP)

Informações

Descrição
Computer Science Mapa Mental sobre Object Oriented Programming (OOP), criado por ajani walker em 25-12-2023.
Criado por
ajani walker
Visualizações
3
Avaliação

Fale Conosco

Sobre o que é sua mensagem?

O que você acha do novo visual?

Você gostou?