-1

According to the python help(hex) call, the function hex() can only receive an integer. But now I realize that it can also receive other types of numbers and I'm curious to know about what feature of python or its modules makes this possible.

The Python console below is of version 3.8 but I received the same output of hex() on 2.7.

>>> hex(0o20)
'0x10'
>>> hex(0b10000)
'0x10'
>>> hex(16)
'0x10'
>>> help(hex)
Help on built-in function hex in module builtins:

hex(number, /)
    Return the hexadecimal representation of an integer.

    >>> hex(12648430)
    '0xc0ffee'
>>>

PD: What does the forward slash on hex(number, /) mean? I tried adding two arguments to the function call but the return states that hex() can only receive one argument.

Acorn
  • 24,970
  • 5
  • 40
  • 69
ibitebyt3s
  • 2,992
  • 2
  • 15
  • 25
  • 6
    All the examples you've shown are different ways of entering an integer in Python. – Jiří Baum Sep 30 '20 at 00:43
  • 1
    Also see https://stackoverflow.com/questions/24735311/what-does-the-slash-mean-in-help-output – Selcuk Sep 30 '20 at 00:48
  • 1
    The slash means that you can't pass the preceding arguments by name, only by position. Initially this was a documentation convention, as of Python 3.8 it's become available for use in code. – Jiří Baum Sep 30 '20 at 00:48

1 Answers1

1

If you look at the syntax for supported radicals, you can see that all your examples in the question are of integers.

For example:

This proposal is to use a "0o" prefix with either uppercase or lowercase "o" for octal, and a "0b" prefix with either uppercase or lowercase "b" for binary.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • Uh, while that *might* still be true today, that's the documentation from over 19 years ago (June 22, 2001). Not the best choice :-) – superb rain Sep 30 '20 at 01:51
  • Actually it's not true anymore. That's lacking the binary literal and the octal literal with `0o` prefix. Making your "Therefore..." wrong. – superb rain Sep 30 '20 at 01:55
  • @superbrain Yep, thanks for the correction. I've updated the answer accordingly. – Ami Tavory Sep 30 '20 at 10:32
  • Hmm, no, you still link to the same outdated page, the quote you show isn't on that page, and it's talking about a *proposal* only (and it doesn't say it's about ints). – superb rain Sep 30 '20 at 11:00
  • Still phrased as a proposal, one needs to go to the page to see that that is a PEP, maybe one that was accepted and implemented like that and that that's still valid (it's from 2007 and is for Python 3.0, after all). I'd just use the current documentation [here](https://docs.python.org/3/reference/lexical_analysis.html#integer-literals). – superb rain Sep 30 '20 at 11:25
  • @superbrain Thanks, added. BTW, I know the rep's not a big deal, but if you answer your comment, I'll happily upvote it. – Ami Tavory Sep 30 '20 at 12:40
  • Well now the quote and the link text again don't match the link :-). I wouldn't have much more to offer in an answer, I'd just fix the quote and link text. – superb rain Sep 30 '20 at 12:51