[Tutor] Some question about OO practice
washakie at gmail.com
Sun Nov 4 14:43:08 CET 2007
One last question (okay, one MORE question ;) ) when you call a
method/function with optionaly *args **kwargs, I'm finding I need to make
the statement like;
def myFunc(self, *args,**kwargs):
In ord to pass the kwargs:
myFunc(**kwargs) # where kwargs is a dictionary
or for args:
It is correct that you need to use the '**' in the 'calling' statement?
On Nov 4, 2007 2:31 PM, Kent Johnson <kent37 at tds.net> wrote:
> John wrote:
> > I've now written my first set of Classes to do some fairly specific
> > processing for work I do. I have a few questions.
> > First, in looking through what I've done, I basically just incorporated
> > all my previous scripts into classes... they are still highly specific
> > to my application, though I did try to make them somewhat 'reusable' or
> > general. It is difficult though, as portions required hardcoding. For
> > that I used the __init__ method to define a bunch of 'hardcoded'
> > variables, that could then be set if they were passed on initiation.
> You didn't show your original script or enough context for me to judge
> if this was a useful change. A program that uses classes rather than
> simple scripts is not necessarily 'better' than one that doesn't.
> Classes are a tool that is helpful in some circumstances but not all. In
> other words don't add classes to your programs just because it seems
> like a good idea. Some good reasons for using classes are here:
> > I guess, I'm writing because I'm wondering now what people think about
> > writing classes versus just using scripts for things that are so
> > specific. It's hard for me to give an example, or show what I'm doing,
> > but I would appreciate thoughts on that matter.
> It's ok for classes to be specific.
> > One thing I am struggling with is how to assign *args and **kwargs if
> > they are passed, and how to ignore them if they are not... right now I
> > do this:
> > def myfunc(self, *args,**kwargs):
> > a=self.a
> > b=self.b
> > kwa=self.kwa
> > kwb=self.kwb
> > try:
> > a=args; b=args
> > kwa=kwargs['a']
> > kwb=kwargs['b']
> > except: pass
> > Where self.X is defined in the __init__ of the class. Is that correct?
> No, this will not assign from kwargs if args is not given. How about this:
> def myfunc(self, *args,**kwargs):
> a=args; b=args
> except TypeError:
> except TypeError:
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor