Struggeling with collections

Roel Schroeven roel at roelschroeven.net
Mon Mar 7 16:55:54 EST 2016


Faling Dutchman schreef op 2016-03-07 09:24:
> class Item:
>     def __init__(self, id, productId, quantity, pageCount, files, option, metadata):
>         self.id = id
>         self.productId = productId
>         self.quantity = quantity
>         self.pageCount = pageCount
>         self.files = files
>         self.option = option
>         self.metadata = metadata
> 
> itm = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
> print(itm)
> 
> it prints: <__main__.Item object at 0x02EBF3B0>

I'm not 100% sure exactly what you need, but namedtuple might be what 
you're looking for:

 >>> import collections
 >>> Item = collections.namedtuple('Item', 'id productId quantity 
pageCount files option metadata')
 >>> itm = Item(1, None, 1, 1, 'asdf', {'asdf': 3, 'ads': 55}, None)
 >>> print(itm)
Item(id=1, productId=None, quantity=1, pageCount=1, files='asdf', 
option={'ads': 55, 'asdf': 3}, metadata=None)

See 
https://docs.python.org/3/library/collections.html?highlight=namedtuple#collections.namedtuple


-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
   -- Isaac Asimov

Roel Schroeven




More information about the Python-list mailing list