내장함수 - hex(x)

2015. 7. 20. 13:04IT관련

반응형

https://docs.python.org/2/library/functions.html#hex


hex(x)

x를 '0x'가 붙은 hexadecimal로 리턴한다.



>>> class foobar():

...     def __init__(self, val):

...             self.val = val

...     def __hex__(self):

...             return int(self.val)

... 

>>> del x

>>> x = foobar(2000)

>>> print x.val

2000

>>> hex(x.val)

'0x7d0'

>>> hex(x)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: __hex__ returned non-string (type int)


x가 object일 경우 __hex__()를 구현하면 된다고 되어 있는데 뭔가 문제가 있는 듯.



반응형

'IT관련' 카테고리의 다른 글

내장함수 - input([prompt])  (0) 2015.07.20
내장함수 - id(obejct)  (0) 2015.07.20
내장함수 - help(object)  (0) 2015.07.20
내장함수 - hash(object)  (0) 2015.07.20
내장함수 - hasattr(object, name)  (0) 2015.07.20