Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   A one liner to dynamically "kwargify" your variables? (http://www.programmingforums.org/showthread.php?t=10715)

Sane Jul 13th, 2006 7:37 PM

A one liner to dynamically "kwargify" your variables?
 
Does anyone know of a way to simply turn your variables in to the format of **kwargs?

I posted this code in the 500 Ways to Program Numbers 1 Through 10! thread a moment ago...

:

def process(data_start, data_limit):
    data_start += 1
    print data_start
    return (), {'data_start':data_start, 'data_limit':data_limit}

def evaluate(data_start, data_limit):
    if data_start == data_limit:
        return False
    return True

def repeat(p, e, *args, **kwargs):
    while e(*args, **kwargs): args, kwargs = p(*args, **kwargs)

repeat(process, evaluate,
      data_start=0, data_limit=10)


Is there any way to replace the part highlighted red, so I don't have to restructure the dictionary? In other words, a way to "kwargify" a set of variables, or create a dictionary where each key is the name of the variable, and its value is its respective value?

Arevos Jul 14th, 2006 6:01 AM

Not that I know of, but you can get all variables in a scope. Since all functions have their own own scope, and since create no new variables, your process function could be replaced with:
:

def process(data_start, data_limit):
    data_start += 1
    print data_start
    return (), vars()

Also, since you're not using *args, you can do away with it. And you don't need if statements when you're only returning boolean values.
:

def process(data_start, data_limit):
    data_start += 1
    print data_start
    return vars()

def evaluate(data_start, data_limit):
    return data_start != data_limit

def repeat(p, e, **kwargs):
    while e(**kwargs): kwargs = p(**kwargs)

repeat(process, evaluate,
      data_start=0, data_limit=10)


Sane Jul 14th, 2006 8:10 AM

Well, the *args were there for the sake of having a more generalized function. But thanks.

DaWei Jul 14th, 2006 8:39 AM

**kwargs is just *argv[] worked over by someone trying to be original? Gotta love it. Now, if you'll excuse me, I'm going off to invent dirt.

Sane Jul 14th, 2006 9:00 AM

Uh wow.

No, Dawei. Kwargs is commonly used. It stands for "KeyWordARGumentS".

I'm not trying to be original. I'm not inventing anything. Even if I were, I wouldn't appreciate that comment either way.

Game_Ender Jul 14th, 2006 9:01 AM

No, I think keyword args is a key value mapping, not an **argv. So its more like a dict, that's not a dict at the same time.

DaWei Jul 14th, 2006 9:03 AM

I know you didn't invent it. I see it in the docs all the time. The comment wasn't directed at Saney-baby, the center of the universe. Incidentally, I don't give a shit what you appreciate.

Sane Jul 14th, 2006 9:05 AM

Uh wow... DaWei? I don't even know what to say...

I understand now that you were referring to the origination of the term kwargs. But would you be able to step back for a moment and see that you're quite out of line with whatever moment you were trailing off with in your last post?

Okay fine, I give up... you win? :confused:


Edit:

@Arevos, I noticed you can call vars().clear() and that seems to destroy vars(). You can't see it anymore, even after assigning more variables. Out of curiosity, do you think that puts any limitations on Python from that point on in the code? Or is vars merely some sort of wrapper that has nothing to do with Python's internal functions?

DaWei Jul 14th, 2006 9:29 AM

"Whatever moment" is just the moment that you think everything revolves around you. That's just your mom, son, not the rest of us.

Arevos Jul 14th, 2006 9:30 AM

Quote:

Originally Posted by DaWei
**kwargs is just *argv[] worked over by someone trying to be original?

Nope. *args is the equivalent of *argv[]. **kwargs has no direct analogy in C.


All times are GMT -5. The time now is 1:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC