Tips03 (python, 規則, property, copy)
命名規則
module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_VAR_NAME, instance_var_name, function_parameter_name, local_var_name.
クラス定義の例
どのクラスからも継承のないクラスは、かならず object を継承します。
class SampleClass(object): """Summary of class here. Longer class information.... Attributes: likes_spam: A boolean indicating if we like SPAM or not. eggs: An integer count of the eggs we have laid. """ def __init__(self, likes_spam=False): """Inits SampleClass with blah.""" self.likes_spam = likes_spam self.eggs = 0 def public_method(self): """Performs operation blah."""
書式=
キーワードの値や初期値のための ‘=‘ 記号の両側には空白は入れません。
# Yes:
def complex(real, imag=0.0): return magic(r=real, i=imag)
# No:
def complex(real, imag = 0.0): return magic(r = real, i = imag)
予約済みの識別子種
_* : この識別子は"from module import *"でimportされません.
__*__ : システムで定義された(system-defined)名前です.これらの名前はインタプリタと(標準ライブラリを含む)実装上で定義されています.アプリケーション側では、この名前規約を使って別の名前を定義しようとすべきではありません.
参考データモデル — Python 2.7ja1 documentation
__* : クラスプライベート(class-private)な名前です.
property
__foo : privateな値
__fooのためのsetter以下のようになる
@property
def foo(self):
return self.__foo
-> hoge.fooとして使える
または
foo = property(get_foo,set_foo)
import copy
copy.copy() : 参照が作られる
copy.deepcopy() : 新しい値が作られる=元を変化させても作ったものは変わらない
文字列配列とリスト
list(string) : 文字列->リスト
"".join(str_list) : リスト->文字列
数値配列とリスト
list(map(int, string)) : 文字列->リスト
"".join(map(str, int_list)) : リスト->文字列