Python

[Django] Http Headerを取得、追加する方法

Djangoでリクエストヘッダやレスポンスヘッダを操作する方法のご紹介

基本

リクエストヘッダはHttpRequestMETA属性、レスポンスヘッダの内容はHttpResponseから辞書と同じ書法で設定、取得できます。

[Python] build-in objectに独自のメソッドを追加する禁断の果実

Pythonはクラスに後からメソッドが追加できる言語です。

class A:
    pass

A().hello()
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# AttributeError: 'A' object has no attribute 'hello'

def hello(self):
    print("hello")

setattr(A,'hello',hello)
A().hello()
# hello

しかし、それはあくまでpythonで定義されたクラスのみで、build-in クラスであるintstrはその限りではありません。