Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
>>> type(True)
<type 'bool'>
>>> type(1)
<type 'int'>
>>> 1 is 1
True
>>> True is 1
False
True будет соответствовать всему, что не является False (0, [], '' и т.д.). Однако если 1 == True даёт True, то 5 == True вернёт False. Есть тут неконсистентность. !!var нужно писать not not var.class Foo(object):
id = 'foo'
id_upper = id.upper()
def foo(self, x):
return id(x)
print get_data(0) # [1]
# лучше в get_data
val = val if not val is None else []
val is not None
вместо val not is None
is и так встречаться не должно…is :)#define NSMALLPOSINTS 257
...
#define NSMALLNEGINTS 5
...
/* Small integers are preallocated in this array so that they
can be shared.
The integers that are preallocated are those in the range
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
>>> import ctypes
>>> ctypes.c_long.from_address(id(7)+24).value=140
>>> print(7+7)
280
>>> import ctypes
>>> ctypes.c_int.from_address(id(7)+16).value=77
>>> print 7 + 7
154
>>> None < 1
True
>>> None < 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()
first, *rest, last = '0123'
print(first, rest, last)
/^(.)(.*)(.)$/std::string first, last = "01";
std::cout << first << ',' << last << std::endl; // , 01
let data => [1, 2, 3, 4, 5];
let [first, ...rest, last] = data();
let first, ...rest, last = data();
К моменту вызова функции переменная pw равна 5
to_pow = [(lambda x: lambda y : y ** x)(x) for x in range(5)]
print(to_pow[2](10)) # 100
11) Использовать классические id, type в функции и класс list в модуле привычным образом не получится.
def list(id=None):
print(__builtins__.id(list))
$ echo 'print __builtins__.id(list)' > a.py
$ python a.py
8692896
$ echo 'import a' > b.py
$ python b.py
Traceback (most recent call last):
File "b.py", line 1, in <module>
import a
File "/home/megabuz/a.py", line 1, in <module>
print __builtins__.id(list)
AttributeError: 'dict' object has no attribute 'id'
import this
def get_data (it = []):
it = it[:]
it.append(1)
return it
Python. Неочевидное поведение некоторых конструкций