Skip to content

Instantly share code, notes, and snippets.

@LouiS0616
Created November 22, 2020 02:16
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 LouiS0616/87296887be14bf3b03598fe885e58814 to your computer and use it in GitHub Desktop.
Save LouiS0616/87296887be14bf3b03598fe885e58814 to your computer and use it in GitHub Desktop.
jsみたいなノリでthisを使うための工夫、らしい
# pyquery.eachで使われていたテクニック?
class Spam:
def highly_func(self, func):
func.__globals__['this'] = self
func()
def __str__(self):
return "it's me!"
class HamEgg:
def highly_func(self, func):
func(self)
def __str__(self):
return "yes, it's me!"
s = Spam()
s.highly_func(lambda: print(this))
he = HamEgg()
he.highly_func(lambda this: print(this))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment