일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 109. Convert Sorted List to Binary Search Tree
- Generator
- concurrency
- iterator
- 43. Multiply Strings
- Protocol
- DWG
- Substring with Concatenation of All Words
- data science
- Decorator
- 파이썬
- Class
- 운영체제
- LeetCode
- Python Implementation
- t1
- 밴픽
- 715. Range Module
- shiba
- 315. Count of Smaller Numbers After Self
- kaggle
- Python Code
- 컴퓨터의 구조
- 프로그래머스
- 30. Substring with Concatenation of All Words
- 시바견
- Convert Sorted List to Binary Search Tree
- Python
- attribute
- Regular Expression
- Today
- Total
목록Computer Science/Python (32)
Scribbling
In this post, we add operator overloading code to custom 'Vector' class in the previous post. https://focalpoint.tistory.com/300 Python: Sequence Protocol To learn sequence protocol in Python, we create a custom vector class. from array import array import math import reprlib class Vector: typecode = 'd' def __init__(self, components): self._component.. focalpoint.tistory.com Vector Class is as ..
Risks of subclassing built-in types - Generally, overriden methods in sub-classes are not called by other built-in methods. class StrangeDict(dict): def __setitem__(self, key, value): super().__setitem__(key, value*2) dic = StrangeDict(one=1) dic['1'] = 1 dic.update(three=3) print(dic) - This is because some methods are written with C-language. - So, use UserDict, UserList, UserString if you nee..
What is interface? Interface is a set of public methods. In python, 'X object', 'X protocol' and 'X interface' have the same meaning. In Python, protocol is pretty dynamic. In below example, FrenchDeck class does not inherit any class (other than default 'object' class). However, the class has sequential protocol as it has two magic methods (__len__, __getitem__). Likewise, in Python, object's d..
The main goals we use 'Abstract Base Class' in python are as follows: - to provide a standerdized way to test whether an object adheres to a certain specification - to prevent any attempt to initiate a subclass that does not override methods of the super class 1> To provide a standerdized way to test whether an object adheres to a certain specification import abc class TypeGroup1(metaclass=abc.A..
To learn sequence protocol in Python, we create a custom vector class. from array import array import math import reprlib class Vector: typecode = 'd' def __init__(self, components): self._components = array(self.typecode, components) def __iter__(self): return iter(self._components) def __repr__(self): components = reprlib.repr(self._components) components = components[components.find('['):-1] ..
What is a "Pythonic Object"? An object with many different magic methods seems to be more pythonic. However, it doesn't necessarily refer to a class with a lot of magic methods. That is, we don't have to implement all the magic methods in every class. Instead, implement them if only needed, because 'being simple' is pythonic. Below is Vector2D class example. from array import array import math c..
ID operator returns the object's memory address. We use it to compare objects' identities. morgan = {'name': 'hoesu', 'age':29} hoesu = morgan print(hoesu is morgan) print(id(hoesu), id(morgan)) "==" operator calls __eq__() method in dict class and checks whether the objects have the same value or not. morgan = {'name': 'hoesu', 'age':29} anonymous = {'name': 'hoesu', 'age':29} print(morgan == a..
Decorator is a callable that takes another function as its parameter. Decorator is executed during 'import time'. The key idea about closure you must understand is that it can access to 'nonlocal' variables outside the function. Take a look at the below exmaple. def make_averager(): series = [] def averager(new_val): series.append(new_val) return sum(series) / len(series) return averager avg = m..
Some traditional design patterns can be simplified by making good use of first-order functions in python. Let's see the below example. 1. Goal: Calculate total price and discount for each order 2. There are three discount strategies; - fidelitypromo: if the customer's fidelity >= 1000, then 5% of the total - bulkitempromo: if q >= 20 for an item, then 10% of the item - largeorderpromo: if type >..
Python memoryview function returns memory view objects. So why use it? Memory view is a safe way to expose 'buffer protocol' in python. Buffer protocol allows an object to expose its inner data without copying. As buffer protocol is only available at the C-API level, memoryview is present to expose the same protocol at Python level. Example1: how memoryview works arr = bytearray('XYZ', 'utf-8') ..