473,405 Members | 2,444 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.

pass data from optparse to other class instances

Hi,
I'm writing a command-line interface using optparse. The cli takes
several options with a single action and several parameters to be used
in the resulting worker classes.

I've been passing parameters from optparse to the workers in two ways:
(1) creating a Globals.py module, set parameters once in the cli code
and read it
when needed in the worker class methods. Something like this:
import Globals
class Foo(object):
def __init__(self):
if Globals.debug:
etc
(2) passing a parameter directly to the worker class __init__ method:
class Bar(object):
def __init__(self, verbose=False):
etc

Are those methods the best/only ways to pass these parameters around?
What's the smart way to do it?
thanks,
--Tim
Jun 27 '08 #1
2 1010
Tim Arnold schrieb:
Hi,
I'm writing a command-line interface using optparse. The cli takes
several options with a single action and several parameters to be used
in the resulting worker classes.

I've been passing parameters from optparse to the workers in two ways:
(1) creating a Globals.py module, set parameters once in the cli code
and read it
when needed in the worker class methods. Something like this:
import Globals
class Foo(object):
def __init__(self):
if Globals.debug:
etc
(2) passing a parameter directly to the worker class __init__ method:
class Bar(object):
def __init__(self, verbose=False):
etc

Are those methods the best/only ways to pass these parameters around?
What's the smart way to do it?
Essentially these are the two ways - and there is not "the" way. Both
approaches are reasonable.

Generally it is better to refuse the temptation to work with global
state - becaues only that ensures that code is de-coupled and more
responsible regarding state.

However there is no need to jump through overly high mounted hoops to
reach that - especially when config-options affect overall program
behaviour, such as verbosity.

So - no clear answer, sorry :)

Diez
Jun 27 '08 #2
On Jun 9, 5:42*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Tim Arnold schrieb:
Hi,
I'm writing a command-line interface using optparse. The cli takes
several options with a single action and several parameters to be used
in the resulting worker classes.
I've been passing parameters from optparse to the workers in two ways:
(1) creating a Globals.py module, set parameters once in the cli code
and read it
when needed in the worker class methods. Something like this:
import Globals
class Foo(object):
* * def __init__(self):
* * * * if Globals.debug:
* * * * * * etc
(2) passing a parameter directly to the worker class __init__ method:
class Bar(object):
* * def __init__(self, verbose=False):
* * * * etc
Are those methods the best/only ways to pass these parameters around?
What's the smart way to do it?

Essentially these are the two ways - and there is not "the" way. Both
approaches are reasonable.

Generally it is better to refuse the temptation to work with global
state - becaues only that ensures that code is de-coupled and more
responsible regarding state.

However there is no need to jump through overly high mounted hoops to
reach that - especially when config-options affect overall program
behaviour, such as verbosity.

So - no clear answer, sorry :)

Diez
Thanks for this info. I'm glad to know my thought process is on the
right track. What if I put all this (optparse, worker classes)
together into a package: I guess then could I have my globals set in
the __init__.py.

Which doesn't buy me that much over just importing Globals.py does it.
I kind-of understand about avoiding globals -- your comment about
coupling helped me understand it more. If I put my often used
functions and some global vars in __init__.py, is that any better than
importing them explicitly from Globals.py?
thanks again,
--Tim
Jun 27 '08 #3

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

Similar topics

5
by: Norbert Thek | last post by:
Hi I'm using Python 24 on Windows > (2k) Is there an easy way to convince optparse to accept newline in the helpstring? and more importand also in the 'desc' string. I tried everything (from...
3
by: Brad Navarro | last post by:
OK, I may be asking for the impossible, but I'll give it a shot: For a case like this: using System.Data.SqlClient; abstract class Base { private SqlConnection DBConnect; Base(string...
9
by: Jay Douglas | last post by:
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error (Cannot pass '<this>' as a ref or out argument because...
17
by: News | last post by:
Hi everyone, My goal is to pull command switches/options from a file and then assign the values to select variables which would eventually be included in a class object. The data file looks...
0
by: Steven Bethard | last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post to see what other people have been doing for this. In general, I love the optparse interface, but it doesn't do any checks...
1
by: Pupeno | last post by:
Hello, I am doing some extreme use of optparse, that is, extending it as explained on http://docs.python.org/lib/optparse-other-reasons-to-extend-optparse.html I have subclassed OptionParser and...
4
by: Count László de Almásy | last post by:
Is there a standard way with optparse to include a blurb of text after the usage section, description, and the list of options? This is often useful to include examples or closing comments when...
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
12
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....
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.