Created
November 22, 2020 02:16
-
-
Save LouiS0616/87296887be14bf3b03598fe885e58814 to your computer and use it in GitHub Desktop.
jsみたいなノリでthisを使うための工夫、らしい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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