Skip to content

Instantly share code, notes, and snippets.

@firefirer1983
Last active June 22, 2020 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firefirer1983/c60d9bdf6f76c67580f978878d700eb8 to your computer and use it in GitHub Desktop.
Save firefirer1983/c60d9bdf6f76c67580f978878d700eb8 to your computer and use it in GitHub Desktop.
python_tricks

Python Tricks

property,做装饰器,或做函数来定义属性访问器

class Demo:
	idx = property(get_idx, set_idx)

class Demo:
  @property
  def idx(self):
    return self.get_idx()
  
  @idx.setattri(self)
  def idx(self, value):
    self.set_idx(value)

itertools.count()作为计数器用

my_list = ["xy", 36, 1.72]
i = itertools.count(0)
name = my_list[next(i)]
age = my_list[next(i)]
height = my_list[next(i)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment