Daniel
Daniel

Reputation: 2092

How to refer to pygame colors with unicode_literals on?

What is the correct way to use pygame.Color names when using unicode_literals?

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> pygame.ver
'1.9.2pre'
>>> pygame.Color('red')
(255, 0, 0, 255)
>>> from __future__ import unicode_literals
>>> pygame.Color('red')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid argument

Upvotes: 1

Views: 942

Answers (2)

Mechanical snail
Mechanical snail

Reputation: 30647

When unicode_literals is enabled, Python 2 interprets string literals the same way as Python 3. That is, 'red' is a Unicode string (called unicode in Python 2, str in 3), and b'red' is a bytestring (called str or bytes in Python 2, bytes in Python 3).

Since pygame.Color only accepts a bytestring, pass it b'red':

>>> from __future__ import unicode_literals
>>> pygame.Color('red')
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid argument
>>> pygame.Color(b'red')
(255, 0, 0, 255)

Upvotes: 1

Jan
Jan

Reputation: 735

>>> type('red')
str

>>> from __future__ import unicode_literals

>>> type('red')
unicode

>>> type(str('red'))
str

>>> import pygame

>>> pygame.ver
'1.9.1release'

>>> pygame.Color(str('red'))
(255, 0, 0, 255)

Upvotes: 1

Related Questions