Python
https://www.python.org/static/img/python-logo@2x.png
概要
__init__.py の役割
show directory has python script
initialize when import modules
読み込むmodule名と実行ファイル名が同一の場合エラーが起きるので注意
開発環境
Class
By describing "class: Meta", you can define something that is not defined in forms.ModelForm.
code: (python)
class MessageForm(forms.ModelForm):
class Meta:
model = Message
class CreateGroupForm(forms.Form):
group_name = forms.CharField(max_length=50)
Methods
Special methods
init
str
bool
...
init is used in the first variable definition.
For str etc., when the function str () is called, you can specify the value to return as follows.
code: (python)
class User():
def __init__(self, name="", age=0):
self.name = name
self.age = age
def say_hello(self):
print("Hello, my name is " + self.name)
def __str__(self):
return "name:" + self.name + " age:" + str(self.age)
def __bool__(self):
return True
user = User("hoge", 20)
str(user)
# 'name:hoge age:20'
bool(user)
# True
Modules
future
apply to ver2,3
code: (python)
from __future__ import division, print_function, absolute_import, unicode_literals
Jupiter
convert notebook to html
code:bash
notebook=notebook.ipynb
jupyter nbconvert --to html ${notebook}
run notebook
code:bash
jupyter notebook --ip=* --no-browser
Julia
inspect
code: (python)
import inspect
import requests
inspect.getsource(requests.get)
itertools
vars
code: (python)
vars(obj)
Property
Samples
True == 1, False == 0になる
code:python
print(True + 8)
> 9
print(False + 2)
> 2
Reference