我正在使用最新的稳定PyCharm 2016.1.4和Python 3.6a1.每当我使用"f-strings"(PEP-498)时,PyCharm就会抱怨f它是一个未解决的引用:
PyCharm不支持文字字符串插值吗?或者,我应该单独启用还是配置它?
def f(a, b, *args):
return (a, b, args)
f(a=3, b=5)
(3, 5, ())
Run Code Online (Sandbox Code Playgroud)
然而:
f(a=3, b=5, *[1,2,3])
TypeError: got multiple values for argument 'b'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
任何特殊原因?
使用Python 3词典时,我不得不做这样的事情:
d=dict()
if 'k' in d:
d['k']+=1
else:
d['k']=0
Run Code Online (Sandbox Code Playgroud)
我似乎记得有一种本地方式来做这件事,但是正在查看文档并找不到它.你知道这是什么吗?
我跟进了这个教程:https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html,当我做50 000个请求时,一切正常.但我需要进行1百万个API调用,然后我对此代码有问题:
url = "http://some_url.com/?id={}"
tasks = set()
sem = asyncio.Semaphore(MAX_SIM_CONNS)
for i in range(1, LAST_ID + 1):
task = asyncio.ensure_future(bound_fetch(sem, url.format(i)))
tasks.add(task)
responses = asyncio.gather(*tasks)
return await responses
Run Code Online (Sandbox Code Playgroud)
因为Python需要创建100万个任务,它基本上只是滞后然后Killed在终端中打印消息.是否有任何方法可以使用预先制作的(或列表)网址的发生器?谢谢.
我试图在Python 3.5.2中这样做:
int(204221389795918291262976/10000)
Run Code Online (Sandbox Code Playgroud)
但得到了意想不到的结果: 20422138979591827456
它在Python 2.7.12中工作正常,结果是: 20422138979591829126L
知道为什么Python 3给了我错误的结果吗?
来自Python书籍:
学习Python。第五版,第 #727 页
我读了以下内容:
如果Python在搜索路径上只找到一个字节码文件并且没有找到源代码,那么它只是直接加载字节码;这意味着您可以将程序仅作为字节代码文件发送,并避免发送源代码
但是当在 Python 3.5 上尝试相同的操作时,它不起作用:
~/Python/Module_Test$ cat a.py
a = "abc"
l = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
导入模块'a'创建的字节码文件如下:
~/Python/Module_Test/__pycache__$ ls
a.cpython-35.pyc
Run Code Online (Sandbox Code Playgroud)
现在我删除了该'a.py'文件并从字节码目录中导入了该模块'a':
~/Python/Module_Test/__pycache__$ python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'a'
Run Code Online (Sandbox Code Playgroud)
我什至尝试将字节码目录添加到搜索路径,但仍然无法加载模块:
>>> …Run Code Online (Sandbox Code Playgroud) 经过一段时间的研究,我一直无法找到为什么这个代码在它们全部大写时会在句子中计算"0"大写字母,但是如果我要输入一个包含任何小写字母的句子,那么它将计算大写字母,例如作为:"Hello World".
message = input("Enter your sentence: ")
print("Capital Letters: ", sum(1 for c in message if message.isupper()))
Run Code Online (Sandbox Code Playgroud) 我想创建一个类层次结构,其中有一个Block可以自行实例化的类。然后我有一个List继承自Block并包含所有列表通用方法的类,最后我有继承自 的类OrderedList等。我希望人们能够实例化等,但不能。LableledListListOrderedListList
换句话说,您可以实例化一个普通对象Block,也可以实例化一个继承自 的OrderedList对象,但您不能实例化。ListBlockList
所有对 Google 的尝试都会导致抽象基类,但没有提供适合这种情况的示例,并且我无法推断。
我想定义一个resize(h, w)方法,并且我希望能够以两种方式之一调用它:
resize(x,y)resize(x)在第二次调用中,我希望y等于x. 我可以在方法定义中执行此操作,还是应该执行类似操作resize(x,y=None)并在内部进行检查:
if y is None:
y = x
Run Code Online (Sandbox Code Playgroud) 我有一个Foo类变量的类remote.我可以remote使用self.remote?访问类变量吗?
class Foo:
remote = False
def __init__(self):
self.remote = True
@classmethod
def print_remote(cls):
print(cls.remote) #prints False but why?
Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×10
class ×2
aiohttp ×1
args ×1
dictionary ×1
inheritance ×1
pycharm ×1
python-3.5 ×1
python-3.6 ×1
string ×1