Closed
Description
b = 1
def f(a): # Use `operator.itemgetter(b)` instead of defining a function
return a[b]
b = 2
f([1, 2, 3]) # 3
playground
but if we follow that direction:
b = 1
f = operator.itemgetter(b)
b = 2
f([1, 2, 3]) # 2
Original issue:
class A:
def __init__(self):
self.foo = 1
@property
def f(self) -> object: # Use `operator.itemgetter(self.foo)` instead of defining a function
return self[self.foo]
This function references an input in the value position, making it impossible to rewrite as an attribute
class A:
def __init__(self):
self.foo = 1
f = property(operator.itemgetter(self.foo))
And if we define it in the __init__
then it would always use the intial value of foo
:
class A:
def __init__(self):
self.foo = 1
self.f = property(operator.itemgetter(self.foo))