问题标签 [python-2.x]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
35117 浏览

python - Python sort() 函数有哪些参数?

除了 之外还有其他论点key,例如:value

0 投票
4 回答
85048 浏览

python - isinstance('aaa', basestring) 和 isinstance('aaa', str) 有什么区别?

0 投票
4 回答
14561 浏览

python - What does python print() function actually do?

I was looking at this question and started wondering what does the print actually do.

I have never found out how to use string.decode() and string.encode() to get an unicode string "out" in the python interactive shell in the same format as the print does. No matter what I do, I get either

  1. UnicodeEncodeError or
  2. the escaped string with "\x##" notation...

This is python 2.x, but I'm already trying to mend my ways and actually call print() :)

Example:

EDIT:

Why am I asking this? I am sick and tired of encode() errors and realized that since print can do it (at least in the interactive shell). I know that the MUST BE A WAY to magically do the encoding PROPERLY, by digging the info what encoding to use from somewhere...

ADDITIONAL INFO: I'm running Python 2.4.3 (#1, Sep 3 2009, 15:37:12) [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2

However, the results are the same with Python 2.6.2 (r262:71600, Sep 8 2009, 13:06:43) on the same linux box.

0 投票
11 回答
402676 浏览

python - urllib、urllib2、urllib3 和 requests 模块有什么区别?

urllib在 Python 中,urllib2urllib3requests模块之间有什么区别?为什么有三个?他们似乎在做同样的事情......

0 投票
1 回答
7450 浏览

python - 整数除法与底商相比:为什么会出现这个令人惊讶的结果?

今天,Python的//“整数除法”运算符让我感到惊讶:

文档显示为“x 和 y 的(下限)商”。那么,为什么 math.floor(11/1.1) 等于 10,而 11//1.1 等于 9?

0 投票
3 回答
872 浏览

python - 在 Python 2.x 中使用 urllib 进行请求时,有什么方法可以设置请求标头?

我正在尝试使用 urllib 模块在 Python 2.6.4 中发出 HTTP 请求。有没有办法设置请求标头?

我确信使用 urllib2 可以做到这一点,但我更喜欢使用 urllib,因为它看起来更简单。

0 投票
5 回答
14090 浏览

python - 为什么分配给 True/False 不能按我的预期工作?

作为回答另一个问题的一部分,我编写了以下代码,乍一看其行为似乎很奇怪:

谁能解释这种奇怪的行为?我认为这与 Python 的对象模型有关,但我不确定。

它是 Cygwin 下的 2.5.2 版本。

0 投票
7 回答
552305 浏览

python - “u”和“r”字符串标志到底是做什么的,什么是原始字符串文字?

在问这个问题时,我意识到我对原始字符串知之甚少。对于自称是 Django 培训师的人来说,这很糟糕。

我知道什么是编码,而且我知道u''单独做什么,因为我知道什么是 Unicode。

  • 但是具体r''做什么呢?它会产生什么样的字符串?

  • 最重要的是,到底做了ur''什么?

  • 最后,是否有任何可靠的方法可以从 Unicode 字符串返回到简单的原始字符串?

  • 啊,顺便说一句,如果您的系统和文本编辑器字符集设置为 UTF-8,u''实际上有什么作用吗?

0 投票
4 回答
5866 浏览

python - python 2.x 中 unicode 字符串的 string.ascii_letters 等价物?

在标准库的“字符串”模块中,

是否有一个类似的常数可以包括所有被认为是 unicode 字母的东西?

0 投票
5 回答
1884 浏览

python - __cmp__ 方法在 Python 2.x 中没有按预期工作吗?

这里的输出是:

这不是我所期望的:我想说“如果名称字段相等,则两个实例相等”。

如果我只是return False__cmp__函数中,这个报告也是True如此!!如果我 return -1,那么我得到False- 但由于我正在尝试比较字符串,这感觉不对。

我在这里做错了什么?