473,796 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: How best to pass arbitrary parameters from one function to another



On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano
>On Tue, 30 Sep 2008 08:58:15 +0000, John O'Hagan wrote:
>Hi Pythonistas,

I'm looking for the best way to pass an arbitrary number and type of
variables created by one function to another. They can't be global
because they may have different values each time they are used in the
second function.

So far I'm trying to do something like this:
def process_args([list, of, command-line, arguments] ):


If you are trying to process commandline args, I suggest you don't re-
invent the wheel. Have a look at the two standard modules, optparse and
getopt. Of the two, getopt is probably simpler to get started with, but
optparse is more powerful.
Thanks, both to you and Bruno for pointing this out, I'll certainly be using
it in future.
>To answer your more general question, see below.

> do stuff
return {dictionary : of, local : variables }

def main_function( **kwargs ):

do stuff
return result

kw1 = process_args( [some, list] )
kw2 = process_args( [a, different, list] )

for i in main_function( **kw1 ):

kw2[ var1 ] = i
kw2[ var2 ] = len( i )

for j in main_function(* *kw2):

print j

This only seems to work if I specify a default value for every possible
parameter of main_function and also for any others which may be passed
to it, which is a bit tedious because there are very many of them but
only a few are used in any given execution of the program.
Er, yes. Presumably main_function actually does something. So it expects
some arguments, and if the arguments aren't given, then it will fail. To
prevent it failing when arguments aren't given, they must have default
values. So define them, once, and be done with it:
[...snip code example...]
>Default values are a feature, not a problem to be solved.
I take your point, but in this case all necessary parameters are present in
the keyword dictionary, so I guess I was surprised that default values still
need to specified.

Also, the function body tests for the presence of each parameter and only acts
on those that are there, so it does not fail because of a missing argument.
However, I'm sure there's a good reason that it works this way.
>As for your nested for-loops (see above), I'm guessing that you're trying
to copy items from one dictionary kw1 to another kw2, before processing
kw2. You should check out the update method on dictionaries.
[...]

That's also of great use, thank you.

What I'm actually trying to do, though, is add some of the output of one call
of main_function to the arguments of another call of the same function (with
different arguments). It's a long story, but I have a number-crunching
function which produces lists of numbers representing musical notes, either
rhythmic or melodic, to be printed out as a score using Lilypond and/or
played by samples or synthesizers using sox.

The melodic and rhythmic parts work by themselves, but I'm trying to get the
function to feed itself rhythms for its melodies, and vice-versa.

I guess that's already too much information....

Thanks for your help.

John
Sep 30 '08 #1
0 1108

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

Similar topics

136
9460
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
0
3326
by: Zlatko Matiæ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding pass-through queries and parameters. I wanted to have all the code on client, while executing it on...
0
4250
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com. --------------------------------------------------------------------------------
12
1765
by: Perre Van Wilrijk | last post by:
Hi there, When I started using VB6, I used to write classes with properties and functions as following ... Private lngf1 As Long Private strf2 As String Public Property Get f1() As Long f1 = lngf1
4
4014
by: MicroMoth | last post by:
Hi, I'm trying to write a update method, in which when the user clicks the update button the update method is passed 10 form fields. Then a update SQL is run to update the database. My question is whats the best way to pass large numbers of parameters into a method. Ten seems a large number to be passing into and out of a method. Stephen
7
3584
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id - FirstName - LastName - CompanyId (many-to-one )
9
5016
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried something like this, but it did not work, gave core dump. class myclass { public: myclass(){}; ~myclass(){};
12
11115
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
2
1695
by: John O'Hagan | last post by:
Hi Pythonistas, I'm looking for the best way to pass an arbitrary number and type of variables created by one function to another. They can't be global because they may have different values each time they are used in the second function. So far I'm trying to do something like this: def process_args( ):
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10201
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.