Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3441952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:36:51+00:00 2026-05-18T08:36:51+00:00

Defining a function, MyFunction(argument, *args): [do something to argument[arg] for arg in *args] if

  • 0

Defining a function,

MyFunction(argument, *args):
[do something to argument[arg] for arg in *args]

if *args is empty, the function doesn’t do anything, but I want to make the default behavior ‘use the entire set if length of *args == 0’

def Export(source, target, *args, sep=','):
    for item in source:
        SubsetOutput(WriteFlatFile(target), args).send(item[0])

I don’t want to check the length of args on every iteration, and I can’t access the keys of
item in source until the iteration begins…

so i could

if len(args) != 0:
   for item in source:

else
   for item in source:

which will probably work but doesn’t seem ‘pythonic’ enough?

is this (is there) a standard way to approach *args or **kwargs and default behavior when either is empty?

More Code:

def __coroutine(func):
    """
    a decorator for coroutines to automatically prime the routine
    code and method from 'curous course on coroutines and concurrency'
    by david beazley www.dabeaz.com
    """

    def __start(*args, **kwargs):
        cr = func(*args, **kwargs)
        next(cr)
        return cr
    return __start


def Export(source, target, *args, sep=','):
    if args:
        for item in source:
            SubsetOutput(WriteFlatFile(target, sep), args).send(item)
    else:
        for item in source:
            WriteFlatFile(target, sep).send(item)

@__coroutine
def SubsetOutput(target, *args):
    """
    take *args from the results and pass to target

    TODO
    ----
    raise exception when arg is not in result[0]
    """
    while True:
        result = (yield)
        print([result.arg for arg in result.dict() if arg in args])
        target.send([result.arg for arg in result.dict if arg in args])


@__coroutine
def WriteFlatFile(target, sep):
    """
    take set of results to a flat file

    TODO
    ----
    """
    filehandler = open(target, 'a')
    while True:
        result = (yield)
        line = (sep.join([str(result[var]) for
                        var in result.keys()])).format(result)+'\n'
        filehandler.write(line)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:36:52+00:00Added an answer on May 18, 2026 at 8:36 am

    Is there a way to pass an “entire set” argument to SubsetOutput, so you can bury the conditional inside its call rather than have an explicit if? This could be None or [], for example.

    # Pass None to use full subset.
    def Export(source, target, *args, sep=','):
        for item in source:
            SubsetOutput(WriteFlatFile(target), args or None).send(item[0])
    
    # Pass an empty list [] to use full subset. Even simpler.
    def Export(source, target, *args, sep=','):
        for item in source:
            SubsetOutput(WriteFlatFile(target), args).send(item[0])
    

    If not, I would go with the two loop solution, assuming the loop really is a single line. It reads well and is a reasonable use case for a little bit of code duplication.

    def Export(source, target, *args, sep=','):
        if args:
            for item in source:
                SubsetOutput(WriteFlatFile(target), args).send(item[0])
        else:
            for item in source:
                FullOutput(WriteFlatFile(target)).send(item[0])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am defining function func1(); I want it to take some param(s) ie var
When defining a method on a class in Python, it looks something like this:
I have trouble defining a trigger for a MySQL database. I want to change
Maybe I'm totally missing something about even handling in jQuery, but here's my problem.
I'm defining a function that takes another function as a parameter (using Microsoft (R)
I am currently defining regular expressions in order to capture parameters in a URL,
I am having trouble defining the content-type of an element (phonenumber) whilst at the
I'm defining a datagrid's RowDetailsTemplate in the following way: RowDetailsTemplate={StaticResource defaultTemplate} where <UserControl.Resources> <DataTemplate
I find myself defining classes like: struct AngleSize { explicit AngleSize(double radians) : size(radians)
Usually when defining a DAO, you would have a setter for the datasource on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.