3
i = 0x0800

What I understand here is that 0x0800 is a hexadecimal number where '0x' denotes the hex type and the following number '0800' is a 2 byte hexadecimal number. On assigning it to a variable 'i', when its type is checked I got this error

>>> type(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

Here I make out that 'i' is suppose to be an int object. I got more confused when I tried this

>>> print i
2048

What is '2048' exactly .. Can anyone throw some light here ?

2
  • 7
    You assigned something to type, it is not the built-in function. Commented Oct 29, 2015 at 17:43
  • As to how does i = 0x0800 work, see docs.python.org/2/reference/… Commented Oct 29, 2015 at 17:47

2 Answers 2

9

i is an integer, but you redefined type:

>>> i = 0x0800
>>> i
2048
>>> type(i)
<type 'int'>
>>> type = 42
>>> type(i)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del type
>>> type(i)
<type 'int'>

Note the type = 42 line; I created a new global name type and that is found before the built-in. You could also use import __builtin__; __builtin__.type(i) in Python 2, or import builtins; builtins.type(i) in Python 3 to access the original built-in type() function:

>>> import __builtin__
>>> type = 42
>>> __builtin__.type(type)
<type 'int'>
>>> type(type)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del type
>>> type(type)
<type 'type'>

The 0x notation is just one of several ways of specifying an integer literal. You are still producing a regular integer, only the syntax for how you define the value differs here. All of the following notations produce the exact same integer value:

0x0800          # hexadecimal
0o04000         # octal, Python 2 also accepts 0400
0b100000000000  # binary
2048            # decimal

See the Integer Literals reference documentation.

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

1 Comment

My Bad .. Got it ... Thumbs up
0

I'll quickly put the answer that I figured out ....

i = 0x0800 will assign an int equivalent of the hexadecimal number (0800) to i.

So if we go down to pieces this looks like

 >>> i
 2048
 >>> 
 >>> (pow(16,3) * 0) + ( pow(16,2) * 8 ) + (pow (16,1) * 0 ) + (pow(16,0) * 0)
 2048

Comments

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.