is it possible to use getitem inside a method, ie
Class MyClass:
@property
def function(self):
def __getitem__():
...
So I can do
A = MyClass()
A.function[5]
A.function[-1]
Everything is a first-class object in python, so the idea should work (the syntax is off), though I'd suggest making function
its own class with properties in it, and then utilizing it in MyClass, unless you have a very good data-hiding reason to not do so...
I'd like to point out that I'm assuming you want to have function
return a subscriptable thing, not have a subscriptable list of functions. That's a different implementation (also can be done, though.)
For example, a subscriptable thing (you could have made just a list
, too):
# python
> class FooFunc(list):
> pass
> class Foo:
> foofunc = FooFunc()
> f = Foo()
> f.foofunc.append("bar")
> f.foofunc[0]
'bar'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With