![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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()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) |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Well, the *args were there for the sake of having a more generalized function. But thanks.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
**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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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? |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"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 |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |