From c34208ae1f0017fb617bbe62cbaaeeeb16ccf7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Fri, 17 Dec 2021 07:24:00 +0100 Subject: [PATCH] Closure, Class, Pygame --- README.md | 11 +++++------ index.html | 15 +++++++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 442cd95..d61bca9 100644 --- a/README.md +++ b/README.md @@ -825,7 +825,7 @@ import . # Imports a built-in or '/.py'. Closure ------- -**We have a closure in Python when:** +**We have/get a closure in Python when:** * **A nested function references a value of its enclosing function and then** * **the enclosing function returns the nested function.** @@ -857,7 +857,7 @@ from functools import partial >>> multiply_by_3(10) 30 ``` -* **Partial is also useful in cases when function needs to be passed as an argument, because it enables us to set its arguments beforehand.** +* **Partial is also useful in cases when function needs to be passed as an argument because it enables us to set its arguments beforehand.** * **A few examples being: `'defaultdict()'`, `'iter(, to_exclusive)'` and dataclass's `'field(default_factory=)'`.** ### Non-Local @@ -968,8 +968,8 @@ class : print() print(f'{}') raise Exception() -loguru.logger.debug() csv.writer().writerow([]) +logging.warning() ``` #### Repr() use cases: @@ -977,7 +977,6 @@ csv.writer().writerow([]) print([]) print(f'{!r}') >>> -loguru.logger.exception() Z = dataclasses.make_dataclass('Z', ['a']); print(Z()) ``` @@ -1120,7 +1119,7 @@ class MyHashable: ### Sortable * **With total_ordering decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.** * **Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.** -* **When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all elements being equal.** +* **When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.** ```python from functools import total_ordering @@ -3052,7 +3051,7 @@ def run(screen, images, mario, tiles): def update_speed(mario, tiles, pressed): x, y = mario.spd x += 2 * ((D.e in pressed) - (D.w in pressed)) - x -= x // abs(x) if x else 0 + x -= (x > 0) - (x < 0) y += 1 if D.s not in get_boundaries(mario.rect, tiles) else (D.n in pressed) * -10 mario.spd = P(*[max(-limit, min(limit, s)) for limit, s in zip(MAX_SPEED, P(x, y))]) diff --git a/index.html b/index.html index d49aa5f..0190917 100644 --- a/index.html +++ b/index.html @@ -50,7 +50,7 @@
- +
@@ -694,7 +694,7 @@ creature = Creature(point, direction)
  • On a filesystem this corresponds to a directory of Python files with an optional init script.
  • Running 'import <package>' does not automatically provide access to the package's modules unless they are explicitly imported in its init script.
  • -

    #Closure

    We have a closure in Python when:

      +

      #Closure

      We have/get a closure in Python when:

      • A nested function references a value of its enclosing function and then
      • the enclosing function returns the nested function.
      def get_multiplier(a):
      @@ -723,7 +723,7 @@ creature = Creature(point, direction)
       30
       
        -
      • Partial is also useful in cases when function needs to be passed as an argument, because it enables us to set its arguments beforehand.
      • +
      • Partial is also useful in cases when function needs to be passed as an argument because it enables us to set its arguments beforehand.
      • A few examples being: 'defaultdict(<function>)', 'iter(<function>, to_exclusive)' and dataclass's 'field(default_factory=<function>)'.

      Non-Local

      If variable is being assigned to anywhere in the scope, it is regarded as a local variable, unless it is declared as a 'global' or a 'nonlocal'.

      def get_counter():
      @@ -818,14 +818,13 @@ creature = Creature(point, direction)
       

      Str() use cases:

      print(<el>)
       print(f'{<el>}')
       raise Exception(<el>)
      -loguru.logger.debug(<el>)
       csv.writer(<file>).writerow([<el>])
      +logging.warning(<el>)
       

      Repr() use cases:

      print([<el>])
       print(f'{<el>!r}')
       >>> <el>
      -loguru.logger.exception()
       Z = dataclasses.make_dataclass('Z', ['a']); print(Z(<el>))
       
      @@ -943,7 +942,7 @@ Z = dataclasses.make_dataclass('Z', [

      Sortable

      • With total_ordering decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.
      • Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.
      • -
      • When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all elements being equal.
      • +
      • When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.
      from functools import total_ordering
       
       @total_ordering
      @@ -2472,7 +2471,7 @@ SIZE, MAX_SPEED = 50, P(def update_speed(mario, tiles, pressed):
           x, y = mario.spd
           x += 2 * ((D.e in pressed) - (D.w in pressed))
      -    x -= x // abs(x) if x else 0
      +    x -= (x > 0) - (x < 0)
           y += 1 if D.s not in get_boundaries(mario.rect, tiles) else (D.n in pressed) * -10
           mario.spd = P(*[max(-limit, min(limit, s)) for limit, s in zip(MAX_SPEED, P(x, y))])
       
      @@ -2871,7 +2870,7 @@ $ pyinstaller script.py --add-data '<path>:.'