Python raise
和raise from
Python 之间的区别是什么?
try:
raise ValueError
except Exception as e:
raise IndexError
Run Code Online (Sandbox Code Playgroud)
产量
Traceback (most recent call last):
File "tmp.py", line 2, in <module>
raise ValueError
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tmp.py", line 4, in <module>
raise IndexError
IndexError
Run Code Online (Sandbox Code Playgroud)
和
try:
raise ValueError
except Exception as e:
raise IndexError from e
Run Code Online (Sandbox Code Playgroud)
产量
Traceback (most recent call last):
File "tmp.py", line 2, in <module>
raise ValueError …
Run Code Online (Sandbox Code Playgroud) 我有一个脚本,它使用sudo在远程服务器上通过SSH运行另一个脚本.但是,当我输入密码时,它会显示在终端上.(否则它工作正常)
ssh user@server "sudo script"
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么,所以我可以通过SSH键入sudo的密码而不输入密码?
我对Python super()以及继承和属性有一个非常奇怪的问题.一,代码:
#!/usr/bin/env python3
import pyglet
import pygame
class Sprite(pyglet.sprite.Sprite):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = self.x, self.y
@property
def x(self):
return super().x
@x.setter
def x(self, value):
super(Sprite, self.__class__).x.fset(self, value)
self.rect.centerx = value
@property
def y(self):
return super().y
@y.setter
def y(self, value):
super(Sprite, self.__class__).y.fset(self, value)
self.rect.centery = value
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,我想要什么(对我来说似乎是Pythonic)
#super(Sprite, self.__class__).x.fset(self, value)
super().x = value
Run Code Online (Sandbox Code Playgroud)
虽然不起作用
super().x
Run Code Online (Sandbox Code Playgroud)
得到的价值很好.在这种情况下,x是超类的属性,同时定义了fset和fget.那为什么不起作用呢?
命名空间Python包(no __init__.py
)和常规Python包(有__init__.py
)之间的区别是什么,特别是当__init__.py
常规包为空时?
我很好奇,因为最近我忘了制作__init__.py
我制作的包装,我从来没有注意到任何问题.实际上,它们似乎与常规包装相同.
编辑:仅支持Python 3.3的命名空间包(参见PEP 420),所以很自然地,这个问题仅适用于Python 3.
你怎么能得到一个没有约束的类方法?
class Foo:
@classmethod
def bar(cls): pass
>>> Foo.bar
<bound method type.bar of <class '__main__.Foo'>>
Run Code Online (Sandbox Code Playgroud)
编辑:这是python 3.抱歉混乱.
我问这个关于Python的问题,虽然它可能适用于大多数OOP语言.
当我只期望在任何程序中只使用一次数据模型时,我可以使用类/静态方法和属性创建一个类,或者只创建一个常规类并将其实例化一次并仅使用该一个副本.在这种情况下,哪种方法更好,为什么?
使用python,我还可以编写一个模块并像使用类一样使用它.在这种情况下,哪种方式更好,为什么?
示例:我希望有一个中央数据结构来访问/保存数据到文件.
模块方式:
data.py
attributes = something
...
def save():
...
Run Code Online (Sandbox Code Playgroud)
main.py
import data
data.x = something
...
data.save()
Run Code Online (Sandbox Code Playgroud)
上课方式:
class Data:
attributes = something
@classmethod
def save(cls):
Data.x = something
Data.save()
Run Code Online (Sandbox Code Playgroud)
实例方式
class Data:
def save(self):
data = Data()
data.x = something
data.save()
Run Code Online (Sandbox Code Playgroud) 我正在使用适用于Python 的fusepy ctypes库(https://github.com/terencehonles/fusepy)编写一个FUSE文件系统.有没有办法分析保险丝操作?当我尝试使用cProfile运行FUSE的实例化时,我得到不可用的输出:
924 function calls (911 primitive calls) in 5.575 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 5.575 5.575 <string>:1(<module>)
7 0.000 0.000 0.000 0.000 __init__.py:1209(debug)
2 0.000 0.000 0.000 0.000 __init__.py:1221(info)
9 0.000 0.000 0.000 0.000 __init__.py:1452(getEffectiveLevel)
9 0.000 0.000 0.000 0.000 __init__.py:1466(isEnabledFor)
1 0.000 0.000 0.000 0.000 __init__.py:239(load)
1 0.000 0.000 0.000 0.000 __init__.py:267(loads)
1 0.000 0.000 0.000 0.000 __init__.py:363(__getattr__)
1 0.000 0.000 0.000 0.000 __init__.py:370(__getitem__)
1 0.000 …
Run Code Online (Sandbox Code Playgroud) 详细说明,在终端中按Ctrl-C会发生什么?是的,我知道它发送了SIGINT,但是到达那里需要采取哪些步骤?
我做了一些研究,所以我认为我理解了大部分的图片,但不是全部.
为了教学,我假设我们在X会话中运行终端模拟器xterm.终端正在运行Bash shell,并且shell当前正在运行一些由前台中的多个进程组成的长时间运行的管道.
我的问题是,到目前为止我的理解是否正确,xterm如何告诉内核将SIGINT发送到给定控制终端的会话?
我在尝试实现__eq__
我作为 C 扩展编写的 Rect 类时遇到问题。我尝试定义一个名为 的方法__eq__
,但 Python 似乎覆盖了它。
static PyObject *
Rect___eq__(Rect *self, PyObject *other)
{
Rect *rect = (Rect *) other;
if (self->x != rect->x || self->y != rect->y ||
self->width != rect->width || self->height != rect->height) {
Py_RETURN_FALSE;
} else {
Py_RETURN_TRUE;
}
}
static PyMethodDef Rect_methods[] = {
{"__eq__", (PyCFunction)Rect___eq__, METH_VARARGS,
"Compare Rects" },
{NULL} /* Sentinel */
};
Run Code Online (Sandbox Code Playgroud)
似乎无论我做什么,Python 默认都是“is”行为:
>>> a = Rect(1, 2, 3, 4)
>>> b = Rect(1, 2, …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种简单的方法来将Bash完成函数从程序"继承"到脚本中.
假设有一个程序foo
提供自己的Bash完成,这样就$ foo sub<TAB>
完成了$ foo subcommand
.现在我想写一个脚本foo-extra
,它接受相同的参数foo
,在调用之前做一些额外的事情foo <arguments>
.使foo-extra
脚本foo
完成函数的最简单方法是什么?
我明白之间的区别
(defpackage :foo
(:export :bar))
Run Code Online (Sandbox Code Playgroud)
和
(defpackage :foo
(:export #:bar))
Run Code Online (Sandbox Code Playgroud)
是后者不会实习bar
到包中KEYWORD
。我的问题是,这样做有意义吗?毕竟,这个包的目的似乎KEYWORD
是用于实习关键字。