我很难搞清楚这一点,它是关于在Python 2.7中引发异常时可以做的错误:
try:
raise [1, 2, 3, 4]
except Exception as ex:
print ex
Run Code Online (Sandbox Code Playgroud)
这里的消息是"异常必须是旧式类或派生自BaseException,而不是列表" - 这部分没问题,但当我将其更改为元组时,我感到困惑:
try:
raise (1, 2, 3, 4)
except Exception as ex:
print ex
Run Code Online (Sandbox Code Playgroud)
这里的消息是"异常必须是旧式类或派生自BaseException,而不是int" - 为什么它被解释为引发int,而不是元组?
Futhermore:
try:
raise (Exception, 'a message')
except Exception as ex:
print ex
Run Code Online (Sandbox Code Playgroud)
在这里,我们实际上提出了一个异常(与之前的示例相比,我们提出了一个int的一致行为) - 我简单地认为这只是另一种方式:
try:
raise Exception, 'a message'
except Exception as ex:
print ex
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,"消息"正被传递给Exceptions ctor(如docs.python.org上所述)
有人可以解释第二和第三种情况,并可能指出我在解释器中编写代码吗?
相关Python 日志记录默认写入 stdout 或 stderr 吗?但我想知道为什么它默认为默认stderr
值。
stderr
对我来说,为什么是默认值并不是很明显。我注意到运行时出了点问题python script.py | tee out.log
,最终日志文件为空。现在我知道可以通过python script.py 2>&1 | tee out.log
或 使用stream
参数来解决:
logging.basicConfig(stream=sys.stdout)
Run Code Online (Sandbox Code Playgroud)
在那次事件之后,我认为在每个脚本中更改默认值以避免再次感到惊讶是合理stream
的stdout
。这是一个好的做法吗?我会错过什么吗?
简化的情况
public class A {
protected A() { }
protected A Make() { return new A(); }
}
public class B : A {
A a = new A(); //inaccessible due to protection level
B b = new B();
private B()
{
A c = new A();//inaccessible due to protection level
a = new A(); //inaccessible due to protection level
a = Make();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么使用A类保护的构造函数在B类中创建A的实例是不可能的?
在我看来,受保护的构造函数就像受保护的方法,因此应该可以在子类中运行它.
pytest
设置主要基于fixtures,我喜欢这种方法,因为它更原子、更明确,并且在xUnit 中限制类继承更少。是否有任何约定或指南来制作固定装置而不是辅助功能?1考虑代码:
def create_flavored_cake(flavor):
return Cake(flavor)
def test_chocolate():
cake = create_flavored_cake('chocolate')
assert cake.is_yummy()
def test_broccoli():
cake = create_flavored_cake('broccoli')
assert cake.is_yummy()
def test_broccoli_with_risins():
cake = create_flavored_cake('broccoli')
cake.add('risins')
assert cake.is_yummy()
Run Code Online (Sandbox Code Playgroud)
应该create_flavored_cake
改成夹具吗?2这种测试具有相同语句但参数不同的场景是否应该触发我将辅助函数转换为夹具以允许更容易的参数化并减少重复?3我应该只将夹具用于设置和拆卸吗?4或者没关系,按照惯例,如果有的话,它应该从一开始就使用fixture?5
我真的不喜欢通过 参数化装置indirect
,但似乎没有更好的方法。那是因为这是一种不好的做法吗?6
我知道我也可以使用pytest.mark.parametrize
哪个看起来更好 IMO。那么我应该在使用夹具、辅助函数和其他一些方法(如果有更好的解决方案)之间划清界限,同时还要考虑对它们进行参数化?7
我已经添加了问题参考的上标。您不必对所有人都给出答案。
案例类似于:Git:丢弃分散的本地分支上的所有更改.
区别:
git push origin foo:foo --force
被拒绝(原点不允许推力).给定提交图:
A--B--C--O1--O2--O3 (foo)
\
L1--L2--L3 (origin/foo)
Run Code Online (Sandbox Code Playgroud)
如果可能,我想实现这个目标:
A--B--C--O1--O2--O3 (foo origin/foo)
Run Code Online (Sandbox Code Playgroud)
如果以上是不可能的,那么以下对我来说是好的:
A--B--C--O1--O2--O3--M1 (foo)
\ /
L1--L2--L3 (origin/foo)
Run Code Online (Sandbox Code Playgroud)
但我希望git diff M1..03
是空的,即丢弃所引入的所有变化origin/foo
.和-s ours
策略并没有帮助,因为这仅适用于冲突和冲突的非-变化仍然会被添加到M1
.
怎么做到这一点?
删除远程分支并推送.git
如果跟踪(检查git branch -vv
),应自动创建远程分支.
为达到这个:
A--B--C--O1--O2--O3 (foo origin/foo)
\
L1--L2--L3
Run Code Online (Sandbox Code Playgroud)
类型:
git push origin --delete foo
git checkout foo # Git ? 1.6.6.
git push origin foo
Run Code Online (Sandbox Code Playgroud) 我无法在Python中生成示例,其中显示了布尔运算符优先级规则以及短路评估.我可以用以下方式显示运算符优先级
print(1 or 0 and 0) # Returns 1 because `or` is evaluated 2nd.
Run Code Online (Sandbox Code Playgroud)
但是当我将其更改为此时,出现短路问题:
def yay(): print('yay'); return True
def nay(): print('nay')
def nope(): print('nope')
print(yay() or nay() and nope()) # Prints "yay\nTrue"
Run Code Online (Sandbox Code Playgroud)
对于每一个的4种可能性时表达式之前or
是True
它是唯一的求值表达式.如果运算符优先级有效,则应打印"nay\nnope\nyay\nTrue"
或"nay\nyay\nTrue"
短路,因为and
应该首先进行评估.
从这个例子中可以想到的是,Python从左到右读取布尔表达式,并在结果已知时结束,无论运算符优先级如何.
我的错误在哪里或者我错过了什么?请举例说明and
第一次评估它是否可见,这不是由于代码从左到右解释.
鉴于课程:
enum ThingEnum { A,B,C}
interface IThing { }
class A : IThing { }
class B : IThing { }
class C: IThing { }
Run Code Online (Sandbox Code Playgroud)
我脑子里有两个实现IThingFactory
.一个使用switch
:
class ThingFactory
{
public IThing MakeThing(ThingEnum type)
{
switch (type)
{
case ThingEnum.A:
return new A();
break;
case ThingEnum.B:
return new B();
break;
case ThingEnum.C:
return new C();
break;
default:
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
另一个使用抽象和方法重载:
class ThingFactory
{
public IThing Make(A a)
{
return new A();
}
public IThing …
Run Code Online (Sandbox Code Playgroud) python ×4
c# ×2
constructor ×1
default ×1
exception ×1
field ×1
fixtures ×1
git ×1
git-merge ×1
git-remote ×1
inheritance ×1
logging ×1
overloading ×1
performance ×1
protected ×1
pytest ×1
python-2.7 ×1
stderr ×1
tuples ×1