Уведомления

Группа в Telegram: @pythonsu

#1 Сен. 11, 2013 10:47:58

lorien
От:
Зарегистрирован: 2006-08-20
Сообщения: 755
Репутация: +  37  -
Профиль  

"Кавычки" в Python

Ребята, int это не тупость, и не издержки утиной типизации. Я бы даже это приведением типов не назвал. Функция int это всего лишь конструктор класса int, он принимает на входе не что угодно, а только int, string или float, о чём вы можете прочитать в exception, которое вылетит, когда вы туда попытаетесь засунуть что-угодно

>>> int(object())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'object'
>>> int(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'NoneType'

вот ещё:
> help(int)
...
Help on class int in module __builtin__:

class int(object)
| int(x[, base]) -> integer
|
| Convert a string or number to an integer, if possible. A floating point
| argument will be truncated towards zero (this does not include a string
| representation of a floating point number!) When converting a string, use
| the optional base. It is an error to supply a base when converting a
| non-string. If base is zero, the proper base is guessed based on the
| string content. If the argument is outside the integer range a
| long object will be returned instead.

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version