Difference between decode and unicode?

Viewed 476

According to this test:

# -*- coding: utf-8 -*-

ENCODING = 'utf-8'

# what is the difference between decode and unicode?
test_cases = [
    'aaaaa',
    'ááááá',
    'ℕℤℚℝℂ',
]
FORMAT = '%-10s %5d %-10s %-10s %5d %-10s %10s'
for text in test_cases :
    decoded = text.decode(ENCODING)
    unicoded = unicode(text, ENCODING)
    equal = decoded == unicoded
    print FORMAT % (decoded, len(decoded), type(decoded), unicoded, len(unicoded), type(unicoded), equal)

There is no difference between .decode() and unicode():

aaaaa          5 <type 'unicode'> aaaaa          5 <type 'unicode'>       True
ááááá          5 <type 'unicode'> ááááá          5 <type 'unicode'>       True
ℕℤℚℝℂ          5 <type 'unicode'> ℕℤℚℝℂ          5 <type 'unicode'>       True

Am I right? If so, why do we have two different ways of accomplishing the same thing? Which one should I use? Is there any subtle difference?

3 Answers
Related