是否有键盘快捷键移动到文件的开头?我检查了IntelliJ的Mac OS X键盘图,它没有列出导航到文件开头的任何内容.
假设我有一个多继承场景:
class A(object):
# code for A here
class B(object):
# code for B here
class C(A, B):
def __init__(self):
# What's the right code to write here to ensure
# A.__init__ and B.__init__ get called?
Run Code Online (Sandbox Code Playgroud)
有写作两种典型的方法C的__init__:
ParentClass.__init__(self)super(DerivedClass, self).__init__()但是,在任何一种情况下,如果父类(A和B)不遵循相同的约定,则代码将无法正常工作(某些可能会被遗漏,或被多次调用).
那么又是什么样的正确方法呢?很容易说"只是保持一致,遵循一个或另一个",但如果A或B来自第三方图书馆,那么呢?有没有一种方法可以确保所有父类构造函数被调用(并且以正确的顺序,只有一次)?
编辑:看看我的意思,如果我这样做:
class A(object):
def __init__(self):
print("Entering A")
super(A, self).__init__()
print("Leaving A")
class B(object):
def __init__(self):
print("Entering B")
super(B, self).__init__()
print("Leaving B")
class …Run Code Online (Sandbox Code Playgroud) 这篇博客在JIRA中有史诗的定义:
史诗是更大的工作机构.Epics是功能级别的工作,包含许多用户故事.使用上面的示例,史诗可能是整个帐户管理功能和查看以前购买的能力.
因此,如果(作为产品所有者)我有一个我希望交付的大型功能,它将包含许多较小的任务并可能跨越冲刺,那么史诗是一个不错的选择.
但是,我可以轻松地创建(使用博客中的示例)"帐户管理"组件,并且与该功能相关的任何任务都会分配该组件.
同样地,我也可以轻松地使用"Account_Management"标签,并且任何属于帐户管理功能的故事/票证都会被标记为该标签.
所以我的问题是:你为什么/什么情况会使用史诗?为什么/你会在什么情况下使用组件?为什么/什么情况下你会使用标签?即 - 所有三个(史诗,标签,组件)似乎服务于非常相似的目的(分组一系列问题),有什么区别?
本文有一个片段__bases__,通过向类继承的类的现有类集合添加一个类来动态更改某些Python代码的继承层次结构.好的,这很难读,代码可能更清晰:
class Friendly:
def hello(self):
print 'Hello'
class Person: pass
p = Person()
Person.__bases__ = (Friendly,)
p.hello() # prints "Hello"
Run Code Online (Sandbox Code Playgroud)
也就是说,Person不从Friendly源级别继承,而是通过修改__bases__Person类的属性在运行时动态添加此继承关系.但是,如果您更改Friendly并Person成为新的样式类(通过继承自object),则会收到以下错误:
TypeError: __bases__ assignment: 'Friendly' deallocator differs from 'object'
Run Code Online (Sandbox Code Playgroud)
关于这一点的谷歌搜索似乎表明,在运行时更改继承层次结构时,新样式类和旧样式类之间存在一些不兼容性.具体来说:"新式类对象不支持赋予其基础属性".
我的问题是,是否有可能通过使用__mro__属性使上述Friendly/Person示例在Python 2.7+中使用新式类工作?
免责声明:我完全意识到这是一个模糊的代码.我完全意识到,在实际的生产代码中,这样的技巧往往难以理解,这纯粹是一个思想实验,并且可以让人们了解Python如何处理与多重继承相关的问题.
在Java 5及更高版本中,你有foreach循环,它可以在任何实现的东西上神奇地工作Iterable:
for (Object o : list) {
doStuff(o);
}
Run Code Online (Sandbox Code Playgroud)
但是,Enumerable仍然没有实现Iterable,这意味着迭代Enumeration你必须执行以下操作:
for(; e.hasMoreElements() ;) {
doStuff(e.nextElement());
}
Run Code Online (Sandbox Code Playgroud)
有谁知道有没有理由Enumeration仍然没有实现Iterable?
我有一个我正在测试的类,它具有另一个类的依赖(其实例被传递给CUT的init方法).我想使用Python Mock库来模拟这个类.
我所拥有的是:
mockobj = Mock(spec=MyDependencyClass)
mockobj.methodfromdepclass.return_value = "the value I want the mock to return"
assertTrue(mockobj.methodfromdepclass(42), "the value I want the mock to return")
cutobj = ClassUnderTest(mockobj)
Run Code Online (Sandbox Code Playgroud)
这很好,但是"methodfromdepclass"是一个参数化方法,因此我想创建一个模拟对象,根据传递给methodfromdepclass的参数,它返回不同的值.
我想要这个参数化行为的原因是我想创建包含不同值的ClassUnderTest的多个实例(其值由mockobj返回的值生成).
有点我在想什么(这当然不起作用):
mockobj = Mock(spec=MyDependencyClass)
mockobj.methodfromdepclass.ifcalledwith(42).return_value = "you called me with arg 42"
mockobj.methodfromdepclass.ifcalledwith(99).return_value = "you called me with arg 99"
assertTrue(mockobj.methodfromdepclass(42), "you called me with arg 42")
assertTrue(mockobj.methodfromdepclass(99), "you called me with arg 99")
cutinst1 = ClassUnderTest(mockobj, 42)
cutinst2 = ClassUnderTest(mockobj, 99)
# now cutinst1 & cutinst2 contain different values …Run Code Online (Sandbox Code Playgroud) 我见过很多Python装饰器的例子:
__init__,__get__和__call__)但我从来没有见过一个可以完成上述所有工作的例子,而且我很难从特定问题的各种答案中综合出来(比如这一个,这一个,或者这一个(它有一个最好的答案)我曾见过SO)),如何结合以上所有内容.
我想要的是一个基于类的装饰器,它可以装饰方法或函数,并且至少需要一个额外的参数.即以下内容可行:
class MyDecorator(object):
def __init__(self, fn, argument):
self.fn = fn
self.arg = argument
def __get__(self, ....):
# voodoo magic for handling distinction between method and function here
def __call__(self, *args, *kwargs):
print "In my decorator before call, with arg %s" % self.arg
self.fn(*args, **kwargs)
print "In my decorator after call, with arg %s" …Run Code Online (Sandbox Code Playgroud) 有一个由Guido写自己半著名的文章暗示,reduce()应该去渡渡鸟的方式,离开了语言.它甚至被降级为Python 3中的顶级函数(而不是填入functools模块中).
对于许多其他功能性编程主食(地图等),可以使用常见的明确替代方案.例如,大多数时候a map()更好地写为列表理解.
我想知道的是,如果有一个类似的"更多pythonic"替代reduce函数.我有一些函数式编程背景(特别是ML),所以reduce()在考虑解决方案时常常会浮现在脑海中,但是如果有更好的方法来实现它们(没有将reduce调用展开到for循环中)我'我想知道.
Python 3中的方法包装器类型是什么?如果我这样定义一个类:
class Foo(object):
def __init__(self, val):
self.val = val
def __eq__(self, other):
return self.val == other.val
Run Code Online (Sandbox Code Playgroud)
然后做:
Foo(42).__eq__
Run Code Online (Sandbox Code Playgroud)
我明白了:
<bound method Foo.__eq__ of <__main__.Foo object at 0x10121d0>>
Run Code Online (Sandbox Code Playgroud)
但如果我这样做(在Python 3中):
Foo(42).__ne__
Run Code Online (Sandbox Code Playgroud)
我明白了:
<method-wrapper '__ne__' of Foo object at 0x1073e50>
Run Code Online (Sandbox Code Playgroud)
什么是"方法包装"类型?
编辑:抱歉更准确:class method-wrapper是类型__ne__,就像我做的那样:
>>> type(Foo(42).__ne__)
<class 'method-wrapper'>
Run Code Online (Sandbox Code Playgroud)
而类型__eq__是:
>>> type(Foo(42).__eq__)
<class 'method'>
Run Code Online (Sandbox Code Playgroud)
此外,method-wrapper似乎是类上任何未定义的魔术方法的类型(因此__le__,如果没有明确定义__repr__,则__str__等也将具有此类型).
我感兴趣的是method-wrapperPython 如何使用该类.类的方法的所有"默认实现"都只是这种类型的实例吗?
假设我在Python中有一个类,它有一个eq方法,用于比较属性是否相等:
class Foo(object):
# init code...
def __eq__(self, other):
# usual eq code here....
Run Code Online (Sandbox Code Playgroud)
然后我如何比较两个Foo实例的参考相等性(即如果它们是同一个实例则测试)?如果我做:
f1 = Foo()
f2 = Foo()
print f1 == f2
Run Code Online (Sandbox Code Playgroud)
即使它们是不同的对象,我也是真的.
python ×6
inheritance ×2
arguments ×1
decorator ×1
dynamic ×1
enumeration ×1
equality ×1
function ×1
intellij-13 ×1
iterable ×1
java ×1
jira ×1
jira-agile ×1
macos ×1
methods ×1
mocking ×1
oop ×1
python-3.x ×1
python-mock ×1
reference ×1
super ×1
types ×1
unit-testing ×1