Почему не там никакой list.clear () метод в Python?

На MySQL.com MD5s перечислены вместе с каждым файлом, который можно загрузить. Например, MySQL "Windows Essentials" 5.1 528c89c37b3a6f0bd34480000a56c372 .

Вы можете загрузка md5 (md5.exe), инструмент командной строки, который вычислит MD5 любого файла, который Вы имеете локально. MD5 точно так же, как любой другой криптографическая хеш-функция , что означает, что данный массив байтов будет всегда производить тот же хеш. Это означает, имеет ли Ваш загруженный zip-файл MySQL (или безотносительно) тот же MD5, как они отправляют на их сайте, у Вас есть тот же самый файл.

39
задан Community 23 May 2017 в 11:53
поделиться

2 ответа

In the threads you linked Raymond Hettinger pretty much sums up the pros and cons of adding that method. When it comes to language design, it's really important to be conservative. See for example the "every feature starts with -100 points" principle the C# team has. You don't get something as clean as Python by adding features willy-nilly. Just take a look at some of the more cruftier popular scripting languages to see where it takes you.

I guess the .clear() method just never did cross the implicit -100 points rule to become something worth adding to the core language. Although given that the methodname is already used for an equivalent purpose and the alternative can be hard to find, it probably isn't all that far from it.

18
ответ дан 27 November 2019 в 02:52
поделиться

I can't answer to the why; but there absolutely should be one, so different types of objects can be cleared with the same interface.

An obvious, simple example:

def process_and_clear_requests(reqs):
    for r in reqs:
        do_req(r)
    reqs.clear()

This only requires that the object support iteration, and that it support clear(). If lists had a clear() method, this could accept a list or set equally. Instead, since sets and lists have a different API for deleting their contents, that doesn't work; you end up with an unnecessarily ugly hack, like:

def process_and_clear_requests(reqs):
    for r in reqs:
        do_req(r)
    if getattr(reqs, "clear"):
        reqs.clear()
    else:
        del reqs[:]

As far as I'm concerned, using del obj[:] or obj[:] = [] are just unpleasant, unintuitive hacks to work around the fact that list is missing clear().

This is taking "reducing redundancy" to a fault, where it damages the consistency of the language, which is even more important.

As to which you should use, I'd recommend del obj[:]. I think it's easier to implement for non-list-like objects.

7
ответ дан 27 November 2019 в 02:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: