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]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Everything is a first-class object in python, so the idea should work (the syntax is off), though I’d suggest making
functionits 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
functionreturn 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):