我正在尝试编写一个Python类,允许我在这样的代码中使用它:
mc = MyClass()
mc.foo()
.bar()
我写过这样的课程:
class MyClass(object):
def __init__(self):
pass
def foo(self):
print "foo called"
return self
def bar(self):
print "bar called"
return self
然而,我能够接近我想要的“漂亮”风格的唯一方法是引入反斜杠(在IPython和IDLE中运行):
>>> mc = MyClass()
>>> mc.foo()\
.bar()
如果没有反斜杠,我有什么办法吗?
答案 0 :(得分:1)
没有。 Python依赖于布局,换行符(除非括在括号中或使用反斜杠转义)终止语句。据我所知,没有办法弯曲它来接受这种语法。
是什么让你想这样做?就个人而言,我会基于可读性来反对它。以下两种选择对我来说都比较清楚,我更喜欢后者(当方法名称足够简短时)。
mc = MyClass()
x = mc.foo()
x.bar()
mc = MyClass()
mc.foo().bar()
在这种情况下,您知道foo()
的结果属于MyClass
类型,您甚至可以按如下方式调用它。如果可能,这绝对是更好的方法(你可以完全省略return语句)。
mc = MyClass()
mc.foo()
mc.bar()
答案 1 :(得分:0)
您可以将命令括在括号中。在有括号的情况下,Python不会“完成”命令。它并不完美,但是如果您链接许多命令,它将很有用:
mc = MyClass()
(mc.foo()
.bar())
答案 2 :(得分:0)
我最近调用了一个流畅的界面,如下所示:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsatisfiedLinkError: com.slytechs.library.NativeLibrary.dlopen(Ljava/lang/String;)J
at com.slytechs.library.NativeLibrary.dlopen(Native Method)
at com.slytechs.library.NativeLibrary.<init>(Unknown Source)
at com.slytechs.library.JNILibrary.<init>(Unknown Source)
at com.slytechs.library.JNILibrary.loadLibrary(Unknown Source)
at com.slytechs.library.JNILibrary.register(Unknown Source)
at com.slytechs.library.JNILibrary.register(Unknown Source)
at com.slytechs.library.JNILibrary.register(Unknown Source)
at org.jnetpcap.Pcap.<clinit>(Unknown Source)
at controller.Controller.initialize(Controller.java:37)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at controller.Main.setLayout(Main.java:31)
at controller.Main.start(Main.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
Exception running application controller.Main
假设foo和bar具有参数(在我的案例中是这样做的),则可以为每个函数的参数指定一行(或更多行):
mc = MyClass()
mc.foo(
).bar(
)