Skip to main content

Log: 202209

· 7 min read

Achievement

  • Fluent Python (2nd ed.)
    • read all! (about 1000 pages)
    • this was really interesting book
      • writing about python from the various perspective
        • comparing with other languages
        • applying various design patterns
        • why python choose this syntax and language features, or why python doesn't do
      • i could learn a lot of general language features, design patterns, architectures and the way to solve problems along with python's language philosophy
      • reviewing the language from only one point of view is poor thinking
        • there are a lot of fields where the language is used
      • along with Essentials of Programming Languages, i knew that how python make it simple and easy to use by hiding low layer implementation such as pointers, treatment of strings, and resource management
      • how easy to use string in python
        • default utf-8 encoding, slice
      • one of the most my favarite point is the syntax to create list and dict
        • [1, 2, 3] and {"x": 1, "y": 2} are almost diagrams
        • to realize what data is, we just look at it, never read it
        • perceivability > readability
      • after all, completely covering python code with type hints is reckless
        • some packages, even pervasive ones, don't offer types enough
        • errors of mypy and pylance are too annoying
        • personally i want to stop using mypy
          • annotate types every parameters and returns, even it's apparent, are bother; optional typing is best choice
          • even variable name, docstring and comments are sufficient to give information of what data is
          • forcing static typing may kill some advantage of python as dynamic language
    • learned
      • decorator using class with __call__()
      • explicit use TypeAlias
      • function singleddispatch
      • typings python supports
        • duck typing
          • avoiding isinstance()
          • using try-catch (EAFP; easier to ask for forgiveness than permission)
            • <-> LBYL; look before you leap
            • hmm i don't like this style lol
            • but it seems that this style has advantage at concurrent programming
        • goose typing
          • using isinstance()
        • static typing
          • mypy
        • static duck typing
          • protocols
      • favor object composition over class inheritance
        • this was also said by JavaScript: The Definitive Guide
      • variance rules of thumb
        • if a formal type parameter defines a type for data that goes into the object after its initial construction, it can be contravariant (strict)
        • if a formal type parameter defines a type for data that comes out of the object, it can be covariant (lenient)
        • e.g. given Callable[[ParamType, ...], ReturnType], each ParamType is contravariant and ReturnType is covariant
      • with statement
        • It’s like factoring out the filling in a sandwich: using tuna with different breads. But what if you want to factor out the bread, to make sandwiches with wheat bread, using a different filling each time? That’s what the with statement offers.

      • python doesn't implement tail recursion optimization (PTC; proper tail calls)
        • Personally, I think it is a fine feature for some languages, but I don’t think it fits Python: the elimination of stack traces for some calls but not others would certainly confuse many users, who have not been raised with tail call religion but might have learned about call semantics by tracing through a few calls in a debugger. - Guido van Rossum

      • coroutines
        • run event loop then manage coroutines in it
          • passing control from one coroutine to another
        • thus coroutine is the way to run the program concurrently in a sigle thread
        • only one coroutine runs at a time
        • pros
          • no data corruption (vs. threads)
          • no cost of memory and context switch (vs. processes)
        • cons
          • cannot be used for CPU-intensive jobs
            • because only one coroutine runs at a time and it blocks the entire process
            • use processes instead
          • cannot be used for file I/O
            • we can use asyncio.to_thread() to avoid blocking the entire process
      • descriptor
        • the way to abstract away property definitions
          • a class that implements a dynamic protocol consisting of the __get__(), __set__(), and __delete__() methods
        • methods in python are also descriptor!
        • Reading an attribute through an instance normally returns the attribute defined in the instance, but if there is no such attribute in the instance, a class attribute will be retrieved. On the other hand, assigning to an attribute in an instance normally creates the attribute in the instance, without affecting the class at all.

      • type class is a metaclass: a class that builds classes
        • type is a subclass of object
        • but at the same time object is an instance of type (what?!)
    • some interesting references
    • some wise words
      • Be conservative in what you send, be liberal in what you accept. - Postel's law, a.k.a the Robustness Principle

      • If you want the sum of a list of items, you should write it in a way that looks like “the sum of a list of items,” not in a way that looks like “loop over these items, maintain another variable t, perform a sequence of additions.” Why do we have high-level languages if not to express our intentions at a higher level and let the language worry about what low-level operations are needed to implement it? - David Eppstein’s

      • When I see patterns in my programs, I consider it a sign of trouble. The shape of a program should reflect only the problem it needs to solve. Any other regularity in the code is a sign, to me at least, that I’m using abstractions that aren’t powerful enough—often that I’m generating by hand the expansions of some macro that I need to write. ​ —​Paul Graham, Lisp hacker and venture capitalist

In Progreess

  • Essentials of Programming Languages

    • Chapter 5. Continuation-Passing Interpreter
      • learned how to optimize tail recursion
      • if recursive function is tail recursion, it no longer need data stored within the current scope of the function for the next call
      • the way passing control flow like data was really fresh idea to me, i had never think like that
      • but i still have a question
        • continuation-passing style actually doesn't leave the function on stack, which is waiting for the value returned from the function called in it
        • but instead, continuations, which is a multi-nested closure, appear as arguments at a function call
        • does this really work as optimization?
  • A Book of Abstract Algebra

    • Chapter 5. Subgroups done!
    • there are many terms that appear in A First Course In Graph Theory
      • i've stopped reading it
        • a lot of terminologies to which i'm not familiar
        • very abstract
        • boring
      • maybe i should've learn abstract algebra first
        • i wanna revenge after learning abstract algebra!

Stack

  • implement something in rust

    • there is demand to rust in the company
      • honestly i think typescript is the best choice for almost all of our product
        • can write both frontend and backend
          • maybe server-side react will come
        • easy to learn and use
        • good googlabiliity
        • cost to learn rust and the strictness don't correspond with our products
        • questionable to implement web apps in such a low-level language
      • but i wanna use rust if used go lang
  • Programming in Haskell

    • putting aside good or bad, i like static typing and functional languages after all
      • really fun to write program in them
    • i wanna read again
  • SQL Antipatterns: Avoiding the Pitfalls of Database Programming

  • Database Internals

  • Computer Organization and Design MIPS Edition: The Hardware/Software Interface (6th ed.)

Music

  • Beautiful World / Utada Hikaru