Simple question on Parameters...

K

KraftDiner

I have defined a method as follows:

def renderABezierPath(self, path, closePath=True, r=1.0, g=1.0, b=1.0,
a=1.0, fr=0.0, fg=0.0, fb=0.0, fa=.25):

Now wouldn't it be simpler if it was:

def renderABezierPath(self, path, closePath=True, outlineColor,
fillColor):

But how do you set default vaules for outlineColor and fillColors?
Like should these be simple lists or should they be structures or
classes...
 
L

Lawrence Oluyede

Il 2005-12-28 said:
I have defined a method as follows:

def renderABezierPath(self, path, closePath=True, r=1.0, g=1.0, b=1.0,
a=1.0, fr=0.0, fg=0.0, fb=0.0, fa=.25):

Now wouldn't it be simpler if it was:

def renderABezierPath(self, path, closePath=True, outlineColor,
fillColor):

But how do you set default vaules for outlineColor and fillColors?

You can make renderABezierPath like this:

def renderABezierPath(self, path, closePath=True,
outlineColor=(1.0, 1.0, 1.0, 1.0), fillColor=(0.0, 0.0, 0.0, 0.25)):

and in the body you unpack the tuples
 
W

Will McGugan

KraftDiner said:
I have defined a method as follows:

def renderABezierPath(self, path, closePath=True, r=1.0, g=1.0, b=1.0,
a=1.0, fr=0.0, fg=0.0, fb=0.0, fa=.25):

Now wouldn't it be simpler if it was:

def renderABezierPath(self, path, closePath=True, outlineColor,
fillColor):

But how do you set default vaules for outlineColor and fillColors?
Like should these be simple lists or should they be structures or
classes...

You could define a class for the r, g, b, a values, or just use tuples
thusly..

def renderABezierPath(self, path, closePath=True, outlineColor=(1.0,
1.0, 1.0, 1.0), fillColor=(0.0, 0.0, 0.0, 0.25)):

It all depends on what you find the most elegant solution. Im guessing
you will use color values a lot, so I would recommend writing a simple
class. Its also more natural to refer to the components of the color by
name, rather than an index to the tuple.


Will McGugan
 
K

KraftDiner

I guess its all good...
I just am not crazy about using fillColor[0] id rather use fillColor.r
So is that a structure I need to define or a class...
I'm kind of new to python what would that class or structure look like?
and then do the default parameters still work?
 
H

Hans Nowak

KraftDiner said:
I guess its all good...
I just am not crazy about using fillColor[0] id rather use fillColor.r

You don't have to use fillColor[0], you can use tuple unpacking to name
the elements of the tuple. E.g.

def renderABezierPath(self, path, closePath=True, outlineColor=(1.0,
1.0, 1.0, 1.0), fillColor=(0.0, 0.0, 0.0, 0.25)):
r, g, b, a = outlineColor
fr, fg, fb, fa = fillColor
...do something with these values...
 
W

Will McGugan

KraftDiner said:
I guess its all good...
I just am not crazy about using fillColor[0] id rather use fillColor.r
So is that a structure I need to define or a class...
I'm kind of new to python what would that class or structure look like?
and then do the default parameters still work?

There are no 'structures', as such in Python (not in the C sense). A
color class could be defined like this..

class Color(object):
def __init__(self, r, g, b, a):
self.r= r
self.g= g
self.b= b
self.a= a

You can use instances of Color as a default parameter, but because
defaults are evaluated once (at import time), it is usualy better to use
None as the default, and act accordingly. eg.

def renderABezierPath(self, path, closePath=True, outlineColor=None,
fillColor=None):
outlineColor = outlineColor or Color(1.0, 1.0, 1.0, 1.0)
fillColor = fillColor or Color(0.0, 0.0, 0.0, 0.25)

Will McGugan
 
F

Fredrik Lundh

Hans said:
You don't have to use fillColor[0], you can use tuple unpacking to name
the elements of the tuple. E.g.

def renderABezierPath(self, path, closePath=True, outlineColor=(1.0,
1.0, 1.0, 1.0), fillColor=(0.0, 0.0, 0.0, 0.25)):
r, g, b, a = outlineColor
fr, fg, fb, fa = fillColor
...do something with these values...

note that you can do the unpacking in the parameter list, if you
prefer:
.... print r, g, b, a
....1 1 1 0.5

(but I'm not sure that combining unpacking and default values is
the best way to write readable code, though...)

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,372
Members
48,064
Latest member
alibsskemoSeAve

Latest Threads

Top