Giter Site home page Giter Site logo

fluent_python's Introduction

Reference Notes

Concepts & Takeaways

  • Leverage built-in dunder methods instead of re-creating the wheel as new methods
  • Abstract Base Classes (ABCs) --> enforces its subclasses to implement specific methods; ensures consistency and reliability of the interface
  • Polymorphism --> provides flexibility and extensibility to parent class methods, thus ensuring interface uniformity

Terminology

  • infix operators -> create new objs and not touching/modifying the operands

Example: This addition function simply reads the operands self and other , but does not modify them and ultimately returns the new object.

class Vector:
                ...
           
    def __add__(self, other):
        x = self.x + other.x
        y = self.y + other.y
        return Vector(x, y)
  • Container sequences -> versatile; can be more memory intensive as it must hold both a ref and the obj in memory, but flexible in that it can hold memory of any dtype
  • Flat sequences -> homogenous data (only single dtype allowed); less memory intensive; stores the value of each item

Special methods

__repr__ --> provides a helpful, string representation of an object for inspection

For example, the output Vector(4, 5) is descriptive of how the Vector class is defined. This output can also now be used as an input too. Without this custom method, we would see <Vector object at 0x...> which isn't helpful or descriptive.

    def __repr__(self):
        return f'Vector({self.x!r}, {self.y!r})'
    
    v1 = Vector(2, 4)
    v2 = Vector(2, 1)
    v3 = v1 + v2
    
>> Vector(4, 5)

Syntax

!r in the __repr__ allows Vector(2, 4) and Vector('2', '4') to be both valid instead of expecting the x, y values to be ints, so the returned string would output the more accurate dtypes of the args

Data Structures

  • Dynamic arrays = lists (mutable)
  • Fixed-size arrays = tuples (immutable)

fluent_python's People

Contributors

ericdwkim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.