Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 14th, 2006, 9:33 AM   #11
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 Sane
@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?
vars() gives you direct access to the references in the current scope. vars().clear() will therefore remove all references in the current scope, which isn't a good idea. So in retrospect it's probably best to copy it before returning it:
return dict(vars())
Arevos is offline   Reply With Quote
Old Jul 14th, 2006, 9:38 AM   #12
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
*args is more like va_list with friends.
free-zombie is offline   Reply With Quote
Old Jul 14th, 2006, 9:57 AM   #13
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,784
Rep Power: 5 Sane is on a distinguished road
So, would that mean that returning vars() (without copying it first), would be handing off variables that refer to memory locations inside that scope? The scope that no longer exists because the function has ended? Or do the variables still exist in memory after the function has ended? I'll play with this a bit. You also didn't really answer my question. I know what's happening, what I don't know is if it has any impact on Python. If vars is actually an essential part of Python, or some sort of representation therein.

Dawei, I've sent you a PM.
Sane is online now   Reply With Quote
Old Jul 14th, 2006, 10:01 AM   #14
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 Sane
So, would that mean that returning vars() (without copying it first), would be handing off variables that refer to memory locations inside that scope? The scope that no longer exists because the function has ended?
The scope is essentially just a dictionary, and it behaves the same as any other object. It's garbage collected only when no references to it exist. Since you're passing vars() out as an argument, a reference to it still exists, and therefore the scope dictionary still exists.

In a sense, you're persisting the scope of a function after the function has ended. Hmm. Interesting to think what you could do with that.
Arevos is offline   Reply With Quote
Old Jul 14th, 2006, 10:09 AM   #15
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,784
Rep Power: 5 Sane is on a distinguished road
Wow, when you step back and it look at it for a second, it's really working on so many dependancy levels. It boggles my mind at least, not saying much however. :p

By the way, I'm not sure if you saw my post edit? It wasn't included in your quote. I'll assume you hadn't seen it.
Sane is online now   Reply With Quote
Old Jul 14th, 2006, 10:16 AM   #16
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 Sane
By the way, I'm not sure if you saw my post edit? It wasn't included in your quote. I'll assume you hadn't seen it.
Quote:
Originally Posted by Sane
You also didn't really answer my question. I know what's happening, what I don't know is if it has any impact on Python. If vars is actually an essential part of Python, or some sort of representation therein.
I'm sorry, I don't understand the question. If you're worried about it affecting the internals (which it shouldn't do beyond the scope of its operation), you can aways copy the data to a nice, safe, normal dict object.
Arevos is offline   Reply With Quote
Old Jul 14th, 2006, 10:28 AM   #17
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,784
Rep Power: 5 Sane is on a distinguished road
I was referring to my earlier question.

Quote:
I noticed you can call vars().clear()...

...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?
I guess what I'm asking is what the hell vars actually is? :p Is it some make belief object that masks what's really happening, or do you really have that much control over the internals of Python?

It seems strange that I can delete "vars" and still be able to declare variables and use them.
Sane is online now   Reply With Quote
Old Jul 14th, 2006, 10:43 AM   #18
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4 Dameon is on a distinguished road
Quote:
Originally Posted by Python Docs
vars( [object])
Without arguments, return a dictionary corresponding to the current local symbol table. With a module, class or class instance object as argument (or anything else that has a __dict__ attribute), returns a dictionary corresponding to the object's symbol table. The returned dictionary should not be modified: the effects on the corresponding symbol table are undefined.
Emphasis my own

Since you aren't supposed to modify it, that leaves the actual implementation with the option to return a proxy/reference to the internal symbol table rather than a copy. Certainly good for efficiency. The point is, it doesn't matter how much "control" you are given as a result of implementation...the behavior is ultimately undefined, so a little hack that happens to work today might not work a few releases from now.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Jul 14th, 2006, 10:47 AM   #19
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
vars() is just a copy of the local variables as changes do nothing. globals() gives you an object who's changes are propagated into the global namespace, but the deletition of a globals() output is completely harmless since python retains a copy and thus only a reference is deleted.

EDIT: ok, I guess my first sentence was wrong.
EDIT2: it looks like python2.3.5 gives you a copy when calling vars, except in the global namespace, where vars acts like globals.
free-zombie 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 11:03 PM.

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