Struggeling with collections

Faling Dutchman henriarends92 at gmail.com
Mon Mar 7 03:58:08 EST 2016


Op maandag 7 maart 2016 09:49:51 UTC+1 schreef Vincent Vande Vyvre:
> Le 07/03/2016 09:24, Faling Dutchman a écrit :
> > Hey folks,
> > I am just starting off in python, but have good knowledge of both Java and C#. Now is the problem that I need to have multiple instances of one dictionary, that is not a problem if you know how many, but now, it is an unknown amount.
> > Some background info:
> > I am making a library for an API. This library must be easy to use for the people who are going to use it. So I am making the models for the data, the connections and so on, so they just have to fill in the gaps. In C# and Java I did it with objects, but they do not work alike in python, or at least that is what I have found.
> > If I do this:
> > 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>
> > So that is not usefull to me. There can be an infinite amount of objects of Item, and it needs to be easy accessable, just like
> > for i in items
> >      print(i)
> > and it has to show all the parameters of the class Item and not say "ive got an object  at this memory address, have a nice day"
> > I hope my question is clear.
> The classes have a dict
> Python 3.2.3 (default, Jun 18 2015, 21:46:42)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> 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
> ...
>  >>> i = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
>  >>> i.__dict__
> {'files': 'asdf', 'option': {'ads': 55, 'asdf': 3}, 'pageCount': 1, 
> 'metadata': None, 'productId': None, 'id': 1, 'quantity': 1}
>  >>>

Thanks, that is at least part of the problem solved. The rest I can figure out myself though. This was the biggest hurdle.



More information about the Python-list mailing list