如何在Python中创建类或方法摘要?
我试着__new__()像这样重新定义:
class F:
def __new__(cls):
raise Exception("Unable to create an instance of abstract class %s" %cls)
Run Code Online (Sandbox Code Playgroud)
但是现在如果我创建一个G继承自F这样的类:
class G(F):
pass
Run Code Online (Sandbox Code Playgroud)
然后我也无法实例化G,因为它调用了它的超类__new__方法.
有没有更好的方法来定义抽象类?
在我的Python应用程序中,我想创建一个既是a staticmethod又是a的方法abc.abstractmethod.我该怎么做呢?
我尝试应用两个装饰器,但它不起作用.如果我这样做:
import abc
class C(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
@staticmethod
def my_function(): pass
Run Code Online (Sandbox Code Playgroud)
我得到一个异常*,如果我这样做:
class C(object):
__metaclass__ = abc.ABCMeta
@staticmethod
@abc.abstractmethod
def my_function(): pass
Run Code Online (Sandbox Code Playgroud)
不强制执行抽象方法.
如何制作抽象静态方法?
*例外:
File "c:\Python26\Lib\abc.py", line 29, in abstractmethod
funcobj.__isabstractmethod__ = True
AttributeError: 'staticmethod' object has no attribute '__isabstractmethod__'
Run Code Online (Sandbox Code Playgroud) 在Python中使用抽象属性实现以下Scala代码的最短/最优雅的方法是什么?
abstract class Controller {
val path: String
}
Run Code Online (Sandbox Code Playgroud)
Controller强制使用子类来定义Scala编译器的"路径".子类看起来像这样:
class MyController extends Controller {
override val path = "/home"
}
Run Code Online (Sandbox Code Playgroud) 为什么decorator不能装饰静态方法或类方法呢?
from decorator import decorator
@decorator
def print_function_name(function, *args):
print '%s was called.' % function.func_name
return function(*args)
class My_class(object):
@print_function_name
@classmethod
def get_dir(cls):
return dir(cls)
@print_function_name
@staticmethod
def get_a():
return 'a'
Run Code Online (Sandbox Code Playgroud)
双方get_dir并get_a导致AttributeError: <'classmethod' or 'staticmethod'>, object has no attribute '__name__'.
为什么decorator依赖属性__name__而不是属性func_name?(Afaik所有函数,包括classmethods和staticmethods,都有func_name属性.)
编辑:我正在使用Python 2.6.
我想有一个抽象类,它强制每个派生类在其__init__方法中设置某些属性。
我已经看了几个不能完全解决我问题的问题,特别是在这里或这里。这看起来很有希望,但我无法使其正常运行。
我假设我想要的结果可能类似于以下伪代码:
from abc import ABCMeta, abstractmethod
class Quadrature(object, metaclass=ABCMeta):
@someMagicKeyword #<==== This is what I want, but can't get working
xyz
@someMagicKeyword #<==== This is what I want, but can't get working
weights
@abstractmethod
def __init__(self, order):
pass
def someStupidFunctionDefinedHere(self, n):
return self.xyz+self.weights+n
class QuadratureWhichWorks(Quadrature):
# This shall work because we initialize xyz and weights in __init__
def __init__(self,order):
self.xyz = 123
self.weights = 456
class QuadratureWhichShallNotWork(Quadrature):
# …Run Code Online (Sandbox Code Playgroud) 在Python 3.6中,假设我有一个抽象类 MyAbstractClass
from abc import ABC, abstractmethod
class MyAbstractClass(ABC):
@property
@abstractmethod
def myProperty(self):
pass
Run Code Online (Sandbox Code Playgroud)
并且有一个类MyInstantiatableClass继承自它。那么,如何myProperty在此类实例化对象时写入属性?我希望既可以设置又可以获取myProperty。以下无效。
from MyAbstractClass import MyAbstractClass
class MyInstantiatableClass(MyAbstractClass):
def __init__(self, desiredValueOfMyProperty):
????
@myProperty.setter
def myProperty(self, desiredValueOfMyProperty): # value coming from __init__
self._myProperty = desiredValueOfMyProperty
Run Code Online (Sandbox Code Playgroud)
还有一个主要功能
from MyInstantiatableClass import MyInstantiatableClass
def main():
MyInstantiatableClass(3) # 3 is the desiredValueOfMyProperty for this instantiation
MyInstantiatableClass(5) # 5 is the desiredValueOfMyProperty for this instantiation
Run Code Online (Sandbox Code Playgroud)