Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 13th, 2006, 7:37 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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?
Sane is online now   Reply With Quote
Old Jul 14th, 2006, 6:01 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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)
Arevos is offline   Reply With Quote
Old Jul 14th, 2006, 8:10 AM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Well, the *args were there for the sake of having a more generalized function. But thanks.
Sane is online now   Reply With Quote
Old Jul 14th, 2006, 8:39 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
**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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 14th, 2006, 9:00 AM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is online now   Reply With Quote
Old Jul 14th, 2006, 9:01 AM   #6
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
Game_Ender is offline   Reply With Quote
Old Jul 14th, 2006, 9:03 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 14th, 2006, 9:05 AM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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?


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?
Sane is online now   Reply With Quote
Old Jul 14th, 2006, 9:29 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"Whatever moment" is just the moment that you think everything revolves around you. That's just your mom, son, not the rest of us.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 14th, 2006, 9:30 AM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically set variables? titaniumdecoy Python 1 Jul 11th, 2006 7:09 PM
When to use the new keyword in C++? titaniumdecoy C++ 28 Mar 16th, 2006 12:36 PM
Variables coldDeath Python 4 Aug 9th, 2005 11:35 AM
Assignment of numbers to variables without asking Haz C# 26 May 23rd, 2005 10:30 AM
global variables uman C++ 2 Feb 20th, 2005 10:39 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:57 PM.

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