list element of a class with the for statement

Anton Vredegoor anton at vredegoor.doge.nl
Tue Mar 26 06:29:43 EST 2002


On Tue, 26 Mar 2002 08:34:41 -0000, "cedric_briner"
<briner at infomaniak.ch> wrote:

>class CRdb:
>	__fieldsName= []
>	__keyname= []
>	__records   = ['first','secnd','third']
>	__iRecord= 0
>
>because I would like to write something like:
>
>db=CRdb()
>for entry in db:
>	print entry

This question should be answered by RTFM! But anyway,  just to give a
starting point:

class CRdb:

    def __init__(self):
        self.__fieldsName= []
        self.__keyname= []
        self.__records   = ['first','secnd','third']
        self.__iRecord= 0

    def __getitem__(self,i):
        return self.__records[i]

def test():
    db=CRdb()
    for entry in db:
        print entry

if __name__=='__main__':
    test()




More information about the Python-list mailing list