does python has a way to implement C#-like indexers? -


this question has answer here:

how implement in python c# indexers-like this[int i] {get set}? in other words, how write follow code in python?

public class foo{     ...     list<foo2> objfoo;     ...     public foo2 this[int i] {       get{ return objfoo[i]; }} }//end class foo  //in class or method... ofoo = new foo(); ... foo2 x = ofoo[3]; 

if want make class work python list (which similar arrays in other languages), can subclass list , give class custom methods , properties without overriding of list methods or properties unless need to.

trying mimic of you've posted:

class foo(list):     def __init__(self, *args):         self.extend(args)         self.x = "now"      def smack(self):         print("smackity!")  ofoo = foo(1, 2, 3, 4, 5, 6) print(ofoo[3])  # prints 4 print(ofoo.x)   # prints "now" ofoo.smack()    # prints "smackity!" 

you can use normal list methods add or change indexed items way want.

also, here's more on lists, if helps.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -