[Python-bugs-list] [ python-Bugs-690611 ] String formatting error - From global scope

SourceForge.net noreply@sourceforge.net
Fri, 21 Feb 2003 19:51:32 -0800


Bugs item #690611, was opened at 2003-02-21 06:34
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=690611&group_id=5470

Category: Parser/Compiler
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Lars Petersen (daath)
Assigned to: Nobody/Anonymous (nobody)
Summary: String formatting error - From global scope

Initial Comment:
This small script fails:

s = 'hejsa'

def main():
    print '\n%(s)s' % vars()

main()

The error is KeyError: s - it works if you move s into the 
function scope...
This does not fail however:

s = 'hejsa'

def main():
    print '\n%s' % (s)

main()


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2003-02-21 22:51

Message:
Logged In: YES 
user_id=31435

logistix's explanation is on target, so closing this as 
NotABug.  Python should probably grow another function 
representing the entire visible namespace via a dict-like 
object, but that would be a new feature request (and not a 
bug).

----------------------------------------------------------------------

Comment By: logistix (logistix)
Date: 2003-02-21 19:43

Message:
Logged In: YES 
user_id=699438

This isn't a bug.  vars() only returns local variables.  If you did:

def main():
    print '\n%(s)s' % globals()

if would work.

>>> help(vars)
Help on built-in function vars:

vars(...)
    vars([object]) -> dictionary
    
    Without arguments, equivalent to locals().
    With an argument, equivalent to object.__dict__.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=690611&group_id=5470