473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Simple question on Parameters...

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...

Dec 28 '05 #1
7 969
Il 2005-12-28, KraftDiner <bo*******@yahoo.com> ha scritto:
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
--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Dec 28 '05 #2
KraftDiner wrote:
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
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Dec 28 '05 #3
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?

Dec 28 '05 #4
KraftDiner wrote:
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...
--
Hans Nowak
http://zephyrfalcon.org/
Dec 28 '05 #5
NICE! :)

Dec 28 '05 #6
KraftDiner wrote:
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
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Dec 28 '05 #7
Hans Nowak wrote:
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:
def render((r, g, b, a)=(1, 0, 0, 1)): .... print r, g, b, a
.... render() 1 0 0 1 render((1, 1, 1, 0.5))

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>

Dec 29 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
3
by: Nils Magnus Englund | last post by:
Hi, I've made a HttpModule which deals with user authentication. On the first request in a users session, it fetches data from a SQL Server using the following code: using (SqlConnection...
2
by: kj | last post by:
I'm sure this is a simple, but recurrent, problem for which I can't hit on a totally satisfactory solution. As an example, suppose that I want write a module X that performs some database...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.