Skip to content

Instantly share code, notes, and snippets.

@odoku
Last active December 31, 2015 01:09
Show Gist options
  • Save odoku/7911902 to your computer and use it in GitHub Desktop.
Save odoku/7911902 to your computer and use it in GitHub Desktop.
Pythonのディスクリプタのサンプル。
class Thumb(object):
def __init__(self, path):
self.url = 'http://hoge.com' + path
self.path = path
class ThumbDiscriptor(object):
BASE_PATH = '/img/{0}/{0}_{1}.{2}'
def __init__(self, name):
self.name = name
def __get__(self, model, klass):
extension = model.image.split('.')[-1]
tmp_path = self.BASE_PATH.format(model.id, self.name, extension)
return Thumb(tmp_path)
class Hoge(object):
id = 10
image = '/img/hoge.jpg'
thumb = ThumbDiscriptor('main_480x360')
c = Hoge()
print c.thumb.url
print c.thumb.path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment