reversed的types是“type”: >>> type(reversed) <class 'type'> sorted的types是“内build函数或方法”: >>> type(sorted) <class 'builtin_function_or_method'> 但是,它们本质上看起来是一样的。 排除function上的明显差异(反转与sorting顺序),这种差异在实现中的原因是什么?
我使用twitter bootstrap和Django。 我有我的依赖由一个点需求文件处理。 我有两个问题: 我在开发时如何less用less量的文件,以便在我编辑less一些的文件时进行编译? 如何创build一种构build脚本来压缩和组合我的JS,并从部署中生成较less的CSS? 我写了一个自定义的构build脚本,创build一个virtualenv,运行“PIP安装-r requirements.txt”,Django syncdb,Django迁移,然后我们走了。 减less融入这个最简单的方法是什么? 谢谢
我很好奇__builtin__模块以及它是如何使用的,但我无法在Python3中find它! 为什么感动? Python 2.7 >>> import __builtin__ >>> Python 3.2 >>> import __builtin__ Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named __builtin__ >>>
PEP 342(通过增强型生成器的协程 throw()为生成器对象添加了一个throw()方法,允许调用者在生成器内引发exception(就像yieldexpression式抛出一样)。 我想知道这个function的用例是什么。
我想了解如何使用DIS(Python字节码的拆解者) 。 具体来说,如何解释dis.dis (或dis.disassemble )的输出? 。 这是一个非常具体的例子(在Python 2.7.3中): dis.dis("heapq.nsmallest(d,3)") 0 BUILD_SET 24933 3 JUMP_IF_TRUE_OR_POP 11889 6 JUMP_FORWARD 28019 (to 28028) 9 STORE_GLOBAL 27756 (27756) 12 LOAD_NAME 29811 (29811) 15 STORE_SLICE+0 16 LOAD_CONST 13100 (13100) 19 STORE_SLICE+1 我看到JUMP_IF_TRUE_OR_POP等是字节码指令(尽pipe有趣的是, BUILD_SET没有出现在这个列表中,但我期望它可以像BUILD_TUPLE一样BUILD_TUPLE ) 。 我认为右边的数字是内存分配,左边的数字是goto数字…我注意到他们每次几乎增加3(但不完全)。 如果我在函数中包装dis.dis("heapq.nsmallest(d,3)") : def f_heapq_nsmallest(d,n): return heapq.nsmallest(d,n) dis.dis("f_heapq(d,3)") 0 BUILD_TUPLE 26719 3 LOAD_NAME 28769 (28769) […]
如果我定义一个小Python程序 class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, 'func', classmethod(self._func)) if __name__ == "__main__": a.func 我收到跟踪错误 Traceback (most recent call last): File "setattr_static.py", line 9, in <module> a.func AttributeError: class a has no attribute 'func' 我想弄清楚的是,我怎样才能dynamic地将类方法设置为一个类,而不需要实例化一个对象呢? 编辑: […]
我想学习Python中的super()函数。 虽然我已经掌握了它,直到我通过这个例子(2.6),发现自己卡住了。 http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html#super-with-classmethod-example Traceback (most recent call last): File "<stdin>", line 1, in <module> File "test.py", line 9, in do_something do_something = classmethod(do_something) TypeError: unbound method do_something() must be called with B instance as first argument (got nothing instead) >>> 当我在这个例子之前阅读这一行时,并不是我所期望的: 如果我们使用一个类方法,我们没有一个实例来调用super。 幸运的是,对于我们来说,super甚至可以使用types作为第二个参数。 —types可以直接传递给super,如下所示。 这正是Python告诉我的,说do_something()应该用B的一个实例调用是不可能的。 提前致谢
嘿,你们都在努力简化我的作业问题,让代码变得更好一些。 我正在使用的是一个二叉search树。 现在我在我的Tree()类中有一个函数,它可以find所有的元素,并把它们放到一个列表中。 tree = Tree() #insert a bunch of items into tree 然后我使用我的makeList()函数从树中的所有节点,并把它们放在一个列表中。 要调用makeList()函数,我做了tree.makeList(tree.root) 。 对我来说这似乎有点重复。 我已经用tree.调用树对象了tree. 所以tree.root只是一个小打字的浪费。 现在makeList函数是: def makeList(self, aNode): if aNode is None: return [] return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild) 我想使aNodeinput一个默认的参数,如aNode = self.root (这是行不通的),我可以运行这个函数, tree.makeList() 。 第一个问题是,为什么不这样做? 第二个问题是,有没有办法可以工作? 正如你所看到的makeList()函数是recursion的,所以我不能在函数的开头定义任何东西,或者我得到一个无限循环。 编辑这里是所有要求的代码: class Node(object): def __init__(self, data): self.data = data self.lChild = […]
我做了一个简单的python脚本在网站上发布数据。 #Imports url_to_short = sys.argv[1] post_url = 'https://www.googleapis.com/urlshortener/v1/url' headers = {'Content-Type': 'application/json'} data = {'longUrl': url_to_short} post_data = json.dumps(data) req = urllib2.Request(post_url, post_data, headers) resp = urllib2.urlopen(req) if resp.getcode() == 200: content = json.loads(resp.read()) #Other stuff 现在我想我们用pylint工具检查脚本的编码标准。 我的pylint输出如下: ************* Module post C: 1,0: Missing docstring C: 6,0: Invalid name "url_to_short" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$) C: 8,0: […]
我正在运行这个简单的代码: import threading, time class reqthread ( threading.Thread ): def __init__ (self): threading.Thread.__init__(self) def run ( self ): for i in range(0,10): time.sleep(1) print '.' try: thread=reqthread() thread.start() except (KeyboardInterrupt, SystemExit): print '\n! Received keyboard interrupt, quitting threads.\n' 但是当我运行它,打印 $ python prova.py ` . . ^C. . . . . . . . Exception KeyboardInterrupt […]