'class' named tuple
I have the following program. I am trying to have index the attributes of an object using __getitem__. Reading them this way works great, but assigning them a value doesn't Is there a way to do such a thing? (Almost like a named tuple, but with custom methods) class LIter(object): def __init__(self,parent=None): super(LIter, self).__init__() self.toto = 3 self.tata = 'terto' def __getitem__(self,index): if index == 0 : return self.toto if index == 1 : return self.tata # [other methods] if __name__ == "__main__": i = LIter() print i[0] print i[1] i.toto = 2 i.tata = 'bye' print i[0] print i[1] i[0] = -1 i[1] = 'salut' print i[0] print i[1] $ python iter.py 3 terto 2 bye Traceback (most recent call last): File "iter.py", line 25, in i[0] = -1 TypeError: 'LIter' object does not support item assignment -- http://mail.python.org/mailman/listinfo/python-list
multiple constructor __init__
Hello all, I would like to instantiate my class as follow QObject(, ) QObject() an example would be http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmenu.html How can I do this without have to specify parent= in the second version (I always need to supply the parent parameter, but I would like to supply it last) I have read this http://stackoverflow.com/questions/356718/how-to-handle-constructors-or-methods-with-a-different-set-or-type-of-argument but all the suggested methods do not work as I want Any idea? -- Emmanuel -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple constructor __init__
Yes, exactly like range http://coverage.livinglogic.de/Demo/classes/Range.py.html see handleargs function. Well that's short, but that's still too much code for what I want to do ;-) On Thu, Feb 2, 2012 at 7:43 PM, Terry Reedy wrote: > On 2/2/2012 8:09 PM, Emmanuel Mayssat wrote: > >> Hello all, >> >> I would like to instantiate my class as follow >> >> >> QObject(, ) >> QObject() >> >> an example would be >> http://www.riverbankcomputing.**co.uk/static/Docs/PyQt4/html/**qmenu.html<http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmenu.html> >> >> How can I do this without have to specify parent= in the second >> version >> (I always need to supply the parent parameter, but I would like to >> supply it last) >> > > The same way range(stop) versus range(start,stop) works. > But I really recommend against that api. > It makes both doc and code messy. > You need a really good reason to not use the obvious > def __init__(self, parent, param=default):... > > -- > Terry Jan Reedy > > -- > http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list> > -- http://mail.python.org/mailman/listinfo/python-list
list of properties
Hello, Is there a way to list 'properties' ? from PyQt4.QtCore import * class LObject(QObject): def __init__(self, parent=None): super(LObject, self).__init__(parent) self.arg1 = 'toto' def getArg2(self): return self.arg1 + " <--" arg2 = property(getArg2) l = LObject() print l.__dict__ returns only arg1 How can I find that arg2 is a 'virtual' attribute (i.e. property)? Regards, -- E -- http://mail.python.org/mailman/listinfo/python-list