添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The base argument to int is meant to be used when parsing a string, not when passing an int to int :

>>> int('ff', 16)

It's a well-established convention to use a, b, c, ..., z for digits representing 10, 11, 12, ..., 35, but there's no convention for what symbol to use for digit 36 in base 37.

First of all: you can only specify a base for int() when converting strings into numbers. Say you have a string with a hexadecimal number, so base 16:

>>> int('2a', 16)

This result differs significantly from the same string interpreted as a different base:

>>> int('2a', 11)
>>> int('2a', 29)

You only ever need the base of an integer number when presenting the value visually or when parsing the integer value from a string representation. You can display an integer value in many different ways, but an int object is the value, not a visual presentations, and doesn't have a base nor can you change that base.

The logical extension when already supporting hexadecimal notation (digits 0-9 and letters A-F), is also support using the letters G-Z, and 10 digits and 26 letters makes it possible to use base 36:

>>> int('zz', 36)

Further bases would need to use non-alphanumeric symbols, for which there is no clear pre-set ordering.

You can't have a base lower than 2; you can't count with just a single digit, the value of 0 will not change.

thanks for explanation, as mathematically it is possible to encode high value, was confused why it's limited here in python. – sahasrara62 Jan 18, 2019 at 18:18

This is a practical implementation convention. What do you envision as the character set for base 100? :-)

Using digits and the entire alphabet is common enough, so the base function with a contiguous set of acceptable base values implemented the functionality that far. Base 1 is simply len(arg).

There is also a convention for base-64 encoding; without a convention for the values 37-63, though, the base function left the radix-64 value for a separate interface.

The base64 encoding conventions (plural!) are incompatible with the usual mapping of symbols to digits, too - for example, the symbol '0' doesn't represent the value 0 in base64 encoding. – user2357112 Jan 18, 2019 at 17:58 Right -- that wouldn't keep base from making the conversion, but the functionality would not be smooth over an apparently small parameter change. – Prune Jan 18, 2019 at 18:13 @Prune i was just experimenting it out . as mathematically it's possible to encode it this high, was checking that in python whether this is possible or not. – sahasrara62 Jan 18, 2019 at 18:16 Of course. It's certainly possible -- but you need to have a convention for the textual representation. There's an obvious interpretation if the input text is limited to the 36 or 64 characters already accepted. However, since we do not have a convention for the other characters in an arbitrarily long set, the built-in functions don't support those bases. Writing your own is easy, if you want that functionality. – Prune Jan 18, 2019 at 18:19

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.