Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104199
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Struggeling with collections |
| Date | 2016-03-07 01:01 -0800 |
| Message-ID | <mailman.11.1457341292.10335.python-list@python.org> (permalink) |
| References | <241aeea9-2604-4fa4-bf06-822ba5be143b@googlegroups.com> |
On 03/07/2016 12:24 AM, Faling Dutchman wrote:
> 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.
It's not clear in the slightest. In fact there isn't even a question
here. Ignoring everything about numbers of objects and libraries and
access (of what?), it seems you want an object of type Item to be able
to print itself nicely. Please correct me if I've got that wrong.
You can do so by defining a member __str__ (or __repr__). Here's a
small example. The returned formatted string can be as simple or
complex as you wish.
>>> class C:
... def __init__(self, a, b):
... self.a = a
... self.b = b
... def __str__(self):
... return "C(a={}, b={})".format(self.a, self.b) # Modify to suit
your needs.
...
>>> print(C(1,2))
C(a=1, b=2)
>>>
Gary Herron
>
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Struggeling with collections Faling Dutchman <henriarends92@gmail.com> - 2016-03-07 00:24 -0800
Re: Struggeling with collections Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2016-03-07 09:39 +0100
Re: Struggeling with collections Faling Dutchman <henriarends92@gmail.com> - 2016-03-07 00:58 -0800
Re: Struggeling with collections Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-07 19:52 +1100
Re: Struggeling with collections Gary Herron <gherron@digipen.edu> - 2016-03-07 01:01 -0800
Re: Struggeling with collections Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-07 11:31 +0200
Re: Struggeling with collections Faling Dutchman <henriarends92@gmail.com> - 2016-03-07 01:43 -0800
Re: Struggeling with collections Roel Schroeven <roel@roelschroeven.net> - 2016-03-07 22:55 +0100
csiph-web