在关闭括号之前/
,Python 3.4的help
输出意味着什么range
?
>>> help(range)
Help on class range in module builtins:
class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return a virtual sequence of numbers from start to stop by step.
|
| Methods defined here:
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
...
Run Code Online (Sandbox Code Playgroud) 看看下面这段代码,它显示了一个列表理解.
>>> i = 6
>>> s = [i * i for i in range(100)]
>>> print(i)
Run Code Online (Sandbox Code Playgroud)
当您在Python 2.6中执行代码示例时,它会打印99,但是当您在Python 3.x中执行它时,它会打印6.
是什么改变行为的原因,以及为什么是输出6中的Python 3.x的?
先感谢您!
我想在文件中保存以下所有异常.我需要这个的原因是因为Ubuntu中的python 3.1.1的IDLE在calltipps上引发了一个Exception,但接近于fast,它不可读.我也需要这个进行测试.最好的,如果我只是调用一个将所有Exception保存到文件的函数.谢谢!;)
//编辑:
我首先看了一个更普通的方式!这样您就不必将整个代码放在函数或缩进中.但现在这对我来说非常好.如果你找到办法,我会感激不尽!
谢谢!
实现高效的Vector/Point类的最佳方法是什么(甚至更好:是否有一个),可以在Python 2.7+和3.x中使用?
我找到了blender-mathutils,但它们似乎只支持Python 3.x. 然后就是这个Vector类,它使用numpy,但它只是一个3D矢量.使用像kivy的矢量类(源代码)的矢量列表,它具有静态属性(x和y)似乎也很奇怪.(有所有这些列表方法.)
目前我正在使用一个扩展了namedtuple的类(如下所示),但这样做的缺点是无法更改坐标.我认为当数千个对象移动并且每次都创建一个新的(矢量)元组时,这可能会成为一个性能问题.(对?)
class Vector2D(namedtuple('Vector2D', ('x', 'y'))):
__slots__ = ()
def __abs__(self):
return type(self)(abs(self.x), abs(self.y))
def __int__(self):
return type(self)(int(self.x), int(self.y))
def __add__(self, other):
return type(self)(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return type(self)(self.x - other.x, self.y - other.y)
def __mul__(self, other):
return type(self)(self.x * other, self.y * other)
def __div__(self, other):
return type(self)(self.x / other, self.y / other)
def dot_product(self, other):
return self.x …
Run Code Online (Sandbox Code Playgroud) 用于__class__
在类中创建新实例是一个好主意吗?
以下代码是执行此操作的示例:
from collections import namedtuple
_Position = namedtuple('Position', ['x', 'y'])
class Position(_Position):
def __add__(self, other):
return __class__(self.x + other.x, self.y + other.y)
Run Code Online (Sandbox Code Playgroud)
使用实际的类名称听起来像重复的代码给我.如果类的名称发生了变化,我必须在所有情况下更改它,即使现代IDE可以为您做到这一点.
顺便说一句.什么样的变量是__class__
?你不应该只能访问它self.
吗?
下面的代码是我首先尝试的,但some_path.with_suffix('.jpg')
显然返回一个pathlib.PosixPath
对象(我在Linux上)而不是我的版本PosixPath
,因为我没有重新定义with_suffix
.我是否必须复制所有内容pathlib
或有更好的方法吗?
import os
import pathlib
from shutil import rmtree
class Path(pathlib.Path):
def __new__(cls, *args, **kwargs):
if cls is Path:
cls = WindowsPath if os.name == 'nt' else PosixPath
self = cls._from_parts(args, init=False)
if not self._flavour.is_supported:
raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,))
self._init()
return self
def with_stem(self, stem):
"""
Return a new path with the stem changed.
The stem is the final path component, minus its last suffix. …
Run Code Online (Sandbox Code Playgroud) 是否可以将update
目录转换为特定版本而无需克隆Mercurial中的整个存储库(本地或中央服务器)以及如何使用它?这将是伟大的,因为clone
整个回购首先花了很多时间给我,文件夹真的不需要整个回购.例如:default和我想要更新的b2.3分支.
提前致谢!:)
是否有一个工具用于创建/编辑具有常量和消息的GWT国际化(i18n)的属性 - 文件(和[可选]接口)?
为什么在调用.pop()时,空集和列表会引发不同的异常?
>>> l = []
>>> l.pop()
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
l.pop()
IndexError: pop from empty list
>>> l = set()
>>> l.pop()
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
l.pop()
KeyError: 'pop from an empty set'
Run Code Online (Sandbox Code Playgroud) 如何对播放列表进行排序,以使同一位艺术家的歌曲尽可能地传播?
尽管我找不到一个或无法解决的问题,但我认为有一种快速简便的方法可以用于各种设置以及两个方面:假设按艺术家和流派进行(非)排序另外一种类型几乎总是跟着另一种类型。(所以这是一个很好的组合)
因此,这是一个示例性播放列表:
from collections import namedtuple
Song = namedtuple('Song', ('artist', 'title', 'length'))
# the length is not correct
Mozart_1 = Song('Mozart', 'Don Giovanni', 3.5)
Mozart_2 = Song('Mozart', 'Serenata Notturna', 2.98)
Mozart_3 = Song('Mozart', 'Violin Concerto No. 3 in G, 1st Movement', 8.43)
Bach_1 = Song('Bach', 'Air', 6.18)
Bach_2 = Song('Bach', 'Toccata in D Minor', 12.44)
Beethoven_1 = Song('Beethoven', 'Für Elise', 2.47)
playlist = [Beethoven_1, Mozart_3, Bach_1, Mozart_2, Mozart_1, Bach_2] # unsorted
Run Code Online (Sandbox Code Playgroud)
这将是一种可能的最佳结果:
OPTIMUM = [Mozart_1, Bach_1, Mozart_2, Beethoven_1, Mozart_3, …
Run Code Online (Sandbox Code Playgroud) 在Kotlin中,您可以为现有类定义扩展方法和属性:
operator inline fun Vector2.plus(other: Vector2) = Vector2(x + other.x, y + other.y)
Run Code Online (Sandbox Code Playgroud)
这允许人们这样做:
val result = Vector2(1.1f, 2.3f) + Vector2(2f, 4f)
Run Code Online (Sandbox Code Playgroud)
有什么办法可以让这个扩展全局化,这样我就不必在每个使用它的类中导入它了吗?
目前,我正在学习科特林并试图创建在所有工作的延伸(中缀)方法号码类型(Byte
,Long
,Float
等)。它应该像 Python 的%
运算符一样工作:
4 % 3 == 1 // only this is the same as Java's %
4 % -3 == -2
-4 % 3 == 2
-4 % -3 == -1
Run Code Online (Sandbox Code Playgroud)
...或者像 Java 的Math.floorMod
,但它也应该与Double
or 一起使用Float
:
-4.3 % 3.2 == 2.1000000000000005
Run Code Online (Sandbox Code Playgroud)
或这些类型的任何可能组合
3 % 2.2 == 0.7999999999999998
3L % 2.2f == 0.7999999999999998
Run Code Online (Sandbox Code Playgroud)
以下按预期工作,但仅适用于两个Double
或两个Int
:
inline infix fun Double.fmod(other: Double): Number { …
Run Code Online (Sandbox Code Playgroud)