DynamicPL/Python
-
Context managerDynamicPL/Python 2019. 11. 9. 23:32
1. Overview Context managers allow you to allocate and release resources precisely when you want to. The most widely used example of context managers is the with a statement. Suppose you have two related operations which you’d like to execute as a pair, with a block of code in between. Context managers allow you to do specifically that. For example: 2. Description 2.1 Context In python, context ..
-
DecoratorsDynamicPL/Python 2019. 11. 7. 18:36
1. Overview In general a decorator function: Takes a function as an argument Returns a closure The closure usually accepts any combination of parameters Runs some code in the inner function (closure) The closure function calls the original function using the arguments passed to the closure Returns whatever is returned by that function call 2. Description 2.1 Decorator function def counter(fn): c..
-
ClosureDynamicPL/Python 2019. 11. 7. 17:09
1. Overview Closure can be consist of a function plus an extended scope that contains the free variables. 2. Description 2.1 Free Variable and Inner Function Functions defined inside another function can access the outer (nonlocal) variables def outer(): x = [1, 2, 3] print('outer:', hex(id(x))) def inner(): print('inner:', hex(id(x))) print(x) return inner fn = outer() fn() x is a free variable..
-
First-Class Object and High-Order functionDynamicPL/Python 2019. 11. 2. 14:02
1. Overview In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity that supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, modified, and assigned to a variable. In mathematics and computer science, a higher-ord..
-
SliceDynamicPL/Python 2019. 10. 28. 15:54
1. Overview Slicing relies on indexing. It only works with sequence types. 2. Description 2.1 Capability on sequence types 2.2 Sequence type 2.3 Slice start and stop bounds 2.4 Step value 2.5 Range equivalence 2.6 Transformation [i:j] 2.7 Transformations [i:j:k], k > 0 2.8 Transformations [i:j:k], k < 0 2.9 indices method 2.10 Summary 3. Example 3.1 Example 1 3.2 Example 2 4. References https://..
-
SequenceDynamicPL/Python 2019. 10. 26. 16:26
1. Definition In Math: $S=x_{1},x_{2},x_{3},\cdots $ Note The sequence of indices: 1, 2, 3, ... We can refer to any item in the sequence by using its index number S[2] So we have a concept of the first element, the second element, and so on Python lists have a concept of positional order, but set do not A list is a sequence type A set is not a sequence type 2. Built-In Sequence Types 2.1 Mutable..
-
List, Dictionary, Set Comprehensions, and Difference between List Comprehensions and Generator expressionsDynamicPL/Python 2019. 10. 22. 16:56
1. Overview After reading this article you’ll learn: What are the list comprehensions in Python What are set comprehensions and dictionary comprehensions 2. Definition List comprehensions provide us with a simple way to create a list based on some iterable. During the creation, elements from the iterable can be conditionally included in the new list and transformed as needed. An iterable is some..
-
iterator, iterable, iteration, iterator protocol, generator functions, generator expressions, and lazy evaluationDynamicPL/Python 2019. 10. 22. 08:44
1. Overview In Python, iterable and iterator have specific meanings. How the iteration in Python works under the hood What are iterables and iterators and how to create them What is the iterator protocol What is a lazy evaluation What are the generator functions and generator expressions 2. Description 2.1 Loop 2.1.1 For Loop Python has something called for loop, but it works like a foreach loop..