[EDIT 00]: I've edited several times the post and now even the title, please read below.

I just learned about the format string method, and its use with dictionaries, like the ones provided by vars(), locals() and globals(), example:

name = 'Ismael'
print 'My name is {name}.'.format(**vars())

But I want to do:

name = 'Ismael'
print 'My name is {name}.' # Similar to ruby

So I came up with this:

def mprint(string='', dictionary=globals()):
    print string.format(**dictionary)

You can interact with the code here: http://labs.codecademy.com/BA0B/3#:workspace

Finally, what I would love to do is to have the function in another file, named my_print.py, so I could do:

from my_print import mprint

name= 'Ismael'
mprint('Hello! My name is {name}.')

But as it is right now, there is a problem with the scopes, how could I get the the main module namespace as a dictionary from inside the imported mprint function. (not the one from my_print.py)

I hope I made myself uderstood, if not, try importing the function from another module. (the traceback is in the link)

It's accessing the globals() dict from my_print.py, but of course the variable name is not defined in that scope, any ideas of how to accomplish this?

The function works if it's defined in the same module, but notice how I must use globals() because if not I would only get a dictionary with the values within mprint() scope.

I have tried using nonlocal and dot notation to access the main module variables, but I still can't figure it out.


[EDIT 01]: I think I've figured out a solution:

In my_print.py:

def mprint(string='',dictionary=None):
    if dictionary is None:
        import sys
        caller = sys._getframe(1)
        dictionary = caller.f_locals
    print string.format(**dictionary)

In test.py:

from my_print import mprint

name = 'Ismael'
country = 'Mexico'
languages = ['English', 'Spanish']

mprint("Hello! My name is {name}, I'm from {country}\n"
       "and I can speak {languages[1]} and {languages[0]}.")

It prints:

Hello! My name is Ismael, I'm from Mexico
and I can speak Spanish and English.

What do you think guys? That was a difficult one for me!

I like it, much more readable for me.


[EDIT 02]: I've made a module with an interpolate function, an Interpolate class and an attempt for a interpolate class method analogous to the function.

It has a small test suite and its documented!

I'm stuck with the method implementation, I don't get it.

Here's the code: http://pastebin.com/N2WubRSB

What do you think guys?


[EDIT 03]: Ok I have settled with just the interpolate() function for now.

In string_interpolation.py:

import sys


def get_scope(scope):
    scope = scope.lower()
    caller = sys._getframe(2)
    options = ['l', 'local', 'g', 'global']

    if scope not in options[:2]:
        if scope in options[2:]:
            return caller.f_globals
        else:
            raise ValueError('invalid mode: {0}'.format(scope))
    return caller.f_locals


def interpolate(format_string=str(),sequence=None,scope='local',returns=False):
    if type(sequence) is str:
        scope = sequence
        sequence = get_scope(scope)
    else:
        if not sequence:
            sequence = get_scope(scope)

    format = 'format_string.format(**sequence)'
    if returns is False:
        print eval(format)

    elif returns is True:
        return eval(format)

Thanks again guys! Any opinions?


[EDIT 04]:

This is my last version, it has a test, docstrings and describes some limitations I've found: http://pastebin.com/ssqbbs57

You can quickly test the code here: http://labs.codecademy.com/BBMF#:workspace

And clone grom git repo here: https://github.com/Ismael-VC/python_string_interpolation.git

有帮助吗?

解决方案 3

Language Design Is Not Just Solving Puzzles: ;)

http://www.artima.com/forums/flat.jsp?forum=106&thread=147358

Edit: PEP-0498 solves this issue!

The Template class from the string module, also does what I need (but more similar to the string format method), in the end it also has the readability I seek, it also has the recommended explicitness, it's in the Standard Library and it can also be easily customized and extended.

http://docs.python.org/2/library/string.html?highlight=template#string.Template

from string import Template

name = 'Renata'
place = 'hospital'
job = 'Dr.'
how = 'glad'
header = '\nTo Ms. {name}:'

letter = Template("""
Hello Ms. $name.

I'm glad to inform, you've been
accepted in our $place, and $job Red
will ${how}ly recieve you tomorrow morning.
""")

print header.format(**vars())
print letter.substitute(vars())

The funny thing is that now I'm getting more fond of using {} instead of $ and I still like the string_interpolation module I came up with, because it's less typing than either one in the long run. LOL!

Run the code here:

http://labs.codecademy.com/BE3n/3#:workspace

其他提示

Modules don't share namespaces in python, so globals() for my_print is always going to be the globals() of my_print.py file ; i.e the location where the function was actually defined.

def mprint(string='', dic = None):
    dictionary = dic if dic is not None else globals()
    print string.format(**dictionary)

You should pass the current module's globals() explicitly to make it work.

Ans don't use mutable objects as default values in python functions, it can result in unexpected results. Use None as default value instead.

A simple example for understanding scopes in modules:

file : my_print.py

x = 10
def func():
    global x
    x += 1
    print x

file : main.py

from my_print import *
x = 50
func()   #prints 11 because for func() global scope is still 
         #the global scope of my_print file
print x  #prints 50

Part of your problem - well, the reason its not working - is highlighted in this question.

You can have your function work by passing in globals() as your second argument, mprint('Hello my name is {name}',globals()).

Although it may be convenient in Ruby, I would encourage you not to write Ruby in Python if you want to make the most out of the language.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top