0

I've executed this from the Python REPL:

>>> ascii('Łukasz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ascii' is not defined
>>> 

Why isn't a built-in function recognized?

The same thing happen when I executed this script with Python interpreter:

#!/user/bin/python
# -*- coding: UTF-8 -*-

def escape_unicode(f):
    def wrap(*args, **kwargs):
        x = f(*args, **kwargs)
        return ascii(x)

    return wrap     

@escape_unicode
def my_name():
    return 'Łukasz'

my_name()

NameError: global name 'ascii' is not defined

1
  • 3
    Are you sure you're in 3.X? ascii doesn't exist in Python 2.7. Commented Oct 27, 2015 at 19:11

2 Answers 2

1

The ascii() function is only available in Python 3.

Use the repr() function, which does exactly what ascii() does in Python 3 (repr() in Python 3 won't use \uhhhh escapes for non-ascii codepoints):

>>> print repr(u'Łukasz')
u'\u0141ukasz'

If you are following a tutorial, you either need to switch to a different tutorial that teaches Python 2, or install Python 3 and continue to use that instead.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm literally just starting with Python. I knew that the tutorial used Python 3.x, but I wrongly assumed that ascii() has been present for a while. Thank you for clarifying.
0

ascii is not a python (2.7) builtin. Perhaps you're thinking of str(), or s.encode('ascii')?

1 Comment

No, they were thinking of using repr().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.