Object-Oriented Programming in Python: Concepts and Implementation, Slides of Programming Languages

python programming language for finance fields

Typology: Slides

2022/2023

Uploaded on 11/07/2023

emelia-goh
emelia-goh 🇹🇭

5 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
1
Object-oriented programming (OOP) is a widely used programming paradigm
Offers various advantages compared to procedural programming
Often well-suited for financial modeling and implementing financial algorithms
Neutral perspective: OOP is an important tool for programmers and quants in finance,
but not always the best choice for every problem
The most important terms are listed as follows:
oClass: An abstract definition of a certain type of objects. For example, a human being
oObject: An instance of a class. For example, Sandra
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Object-Oriented Programming in Python: Concepts and Implementation and more Slides Programming Languages in PDF only on Docsity!

Object-Oriented Programming

  • Object-oriented programming (OOP) is a widely used programming paradigm
  • Offers various advantages compared to procedural programming
  • Often well-suited for financial modeling and implementing financial algorithms
  • Neutral perspective: OOP is an important tool for programmers and quants in finance, but not always the best choice for every problem
  • The most important terms are listed as follows: o Class: An abstract definition of a certain type of objects. For example, a human being o Object: An instance of a class. For example, Sandra

Object-Oriented Programming

  • The most important terms are listed as follows: (Cont.) o Attribute: A feature of the class (class attribute) or of an instance of the class (instance attribute). For example, being a mammal, being a male or female, or color of the eyes o Method: An operation that the class or an instance of the class can implement. For example, walking o Parameters: Input taken by a method to influence its behavior. For example, three steps o Instantiation: The process of creating a specific object based on an abstract class

Object-Oriented Programming

  • There are several human aspects that might speak for the use of OOP:
  • Natural way of thinking o Human thinking typically evolves around real-world or abstract objects, like a car or a financial instrument à OOP is suited to modeling such objects with their characteristics
  • Reducing complexity o OOP helps to reduce the complexity of a problem or algorithm and to model it feature-by-feature

Object-Oriented Programming

  • Nicer user interface o OOP allows a nicer user interfaces and more compact code. This becomes evident, for example, when looking at the NumPy ndarray class or pandas DataFrame class
  • Pythonic way of modeling o Independent of the pros and cons of OOP, it is simply the dominant paragigm in Python. o This is where the saying “everything is an object in Python” comes from. o OOP also slows the programmer to build custom classes whose instances behave like every other instance of a standard Python class

Object-Oriented Programming

  • Modularity o OOP enables modular code organization o Code is divided into multiple, connected modules o Example: modeling a European stock option o Can use one class or two separate classes à Separate classes for underlying stock and option

Object-Oriented Programming

  • Inheritance o Inheritance is a concept where a class inherits attributes and methods from another class. o In finance, this concept can be applied to create a hierarchy of financial instruments. o The hierarchy might start with a general financial instrument, followed by a general derivative instrument, a European option, and finally, a European call option. o Each class in this hierarchy inherits attributes and methods from the higher-level classes.

Object-Oriented Programming

  • Polymorphism o Polymorphism allows multiple forms in programming. o Duck typing is especially important in Python. o Duck typing enables implementing standard operations on various classes without knowing the exact object type. o Example: A financial instruments class can have a method called get_current_price() applicable to different object types (stock, option, swap) without specifying the exact type..

Object-Oriented Programming

  • Encapsulation o This concept refers to the approach of making data within a class accessible only via public methods o A class modeling a stock might have an attribute current_stock_price à encapsulation would than give access to the attribute value via a method get_current_stock_price() and would hide the data from user (i.e. make it private) o This would help avoid unintended effects by simply working with and possible changing attribute values à there are limits to how data can be made private in Python class

Basics of Python Classes

  • This part shows major concepts and the concrete syntax to make use of OOP in Python
  • Let’s build a custom class to model a financial instrument

Basics of Python Classes

  • A special method is init, which gets called during every instantiation of an object
  • It takes as parameter the object itself (self, by convention) and potentially multiple others

Basics of Python Classes

  • Encapsulation aims to hide data within a class
  • Getter and setter methods contribute to this goal
  • Direct access and manipulation of instance attributes still possible
  • This is where Private instance attributes come into play, they are defined by two leading underscores, offer a solution

Basics of Python Classes

  • Cont.

Basics of Python Classes

  • Consider another class that models a portfolio position of financial instrument à two classes aggregation as a concept is illustrated

Python Data Model

  • Previous example highlight some aspect of the so-called Python data or object model
  • The Python data model allows you to design classes that consistency interact with basic language constructs of Python
  • It supports the following tasks and constructs: o Iteration o Collection handling o Attribute access o Operator overloading o Function and method invocation o Object creation and destruction o String representation (e.g. for printing) o Managed contexts (i.e. with blocks)