Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Kent Johnson
Jan Eden wrote: > Now I'll see if I understand the practical difference between items() > and iteritems() - the Python tutorial uses iteritems() in such a > context. iteritems() is actually better usage but a little harder to explain. dict.items() creates a new list with the (key, value) pairs:

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi Kent, hi Alan, Alan G wrote on 07.08.2005: >> page = Show(type=type, id=id) >> >> class Show: >> def __init__(self, type, id): >> self.id = id >> ... >>return self > >First a couple of comments. >You don't need to return self from __init__. >You can only instantiate Show *a

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Alan G
> page = Show(type=type, id=id) > > class Show: > def __init__(self, type, id): > self.id = id > ... >return self First a couple of comments. You don't need to return self from __init__. You can only instantiate Show *after* you've defined it. > For two parameters, this is rel

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Kent Johnson
Jan Eden wrote: > My idea was to transfer the same technique to Python like this: > > class NewClass: > def __init__(self, **parameters): > for i in parameters.keys(): self.i = parameters[i] > > But the assignment in the for loop obviously does not work with > instance attributes. I w

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Jan Eden wrote on 07.08.2005: >But the assignment in the for loop obviously does not work with >instance attributes. I will have to read up some more about instance >attributes. Ok... so this works: class NewClass: def __init__(self, **parameters): self.data = {} for i in par

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi Kent, Kent Johnson wrote on 07.08.2005: >>So how can I add the values of all the paramaters to my class >>instance in one step? > >There was recently a long discussion of this on comp.lang.python. >http://groups.google.com/group/comp.lang.python/browse_frm/thread/ >7346ad00a14e821a/9dc993d2954

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Kent Johnson
Jan Eden wrote: > In Python, I would do: > > page = Show(type=type, id=id) > > class Show: def __init__(self, type, id): self.id = id self.type = > type ... return self > > For two parameters, this is relatively simple. But if I have for > example 10 parameters on instantiation, assigning each v

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi, Jan Eden wrote on 07.08.2005: >So how can I add the values of all the paramaters to my class >instance in one step? > apologies for replying to my own post: I just discovered the **args formal parameter. Thanks, Jan -- I was gratified to be able to answer promptly, and I did. I said I did

[Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi, after using Perl for some years for simple scripting tasks, one of my programs reached a size where an OO design is appropriate. So I rewrote the program using OO techniques in Perl. Because I am not entirely satisfied with the implementation, I decided port the program to Python. The firs