abc

Decorators on abstract methods

元气小坏坏 提交于 2021-02-07 04:44:05
问题 In python, is there a way to make a decorator on an abstract method carry through to the derived implementation(s)? For example, in import abc class Foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @some_decorator def my_method(self, x): pass class SubFoo(Foo): def my_method(self, x): print x SubFoo 's my_method won't get decorated with some_decorator as far as I can tell. Is there some way I can make this happen without having to individually decorate each derived class of Foo ?

Can python abstract base classes inherit from C extensions?

拥有回忆 提交于 2021-02-06 16:28:49
问题 It seems as if that when I have an abstract base class that inherits from gevent.Greenlet (which inherits from the C extension module greenlet: https://github.com/python-greenlet/greenlet) then classes that implement it do not raise any of the abc errors about unimplemented methods. class ActorBase(gevent.Greenlet): __metaclass__ = abc.ABCMeta @abc.abstractmethod def foo(self): print "foo" class ActorBaseTest(ActorBase): def bar(self): print "bar" abt = ActorBaseTest() # no errors! If I

Can python abstract base classes inherit from C extensions?

旧巷老猫 提交于 2021-02-06 16:27:51
问题 It seems as if that when I have an abstract base class that inherits from gevent.Greenlet (which inherits from the C extension module greenlet: https://github.com/python-greenlet/greenlet) then classes that implement it do not raise any of the abc errors about unimplemented methods. class ActorBase(gevent.Greenlet): __metaclass__ = abc.ABCMeta @abc.abstractmethod def foo(self): print "foo" class ActorBaseTest(ActorBase): def bar(self): print "bar" abt = ActorBaseTest() # no errors! If I

Can python abstract base classes inherit from C extensions?

你离开我真会死。 提交于 2021-02-06 16:26:53
问题 It seems as if that when I have an abstract base class that inherits from gevent.Greenlet (which inherits from the C extension module greenlet: https://github.com/python-greenlet/greenlet) then classes that implement it do not raise any of the abc errors about unimplemented methods. class ActorBase(gevent.Greenlet): __metaclass__ = abc.ABCMeta @abc.abstractmethod def foo(self): print "foo" class ActorBaseTest(ActorBase): def bar(self): print "bar" abt = ActorBaseTest() # no errors! If I

Can python abstract base classes inherit from C extensions?

心不动则不痛 提交于 2021-02-06 16:26:47
问题 It seems as if that when I have an abstract base class that inherits from gevent.Greenlet (which inherits from the C extension module greenlet: https://github.com/python-greenlet/greenlet) then classes that implement it do not raise any of the abc errors about unimplemented methods. class ActorBase(gevent.Greenlet): __metaclass__ = abc.ABCMeta @abc.abstractmethod def foo(self): print "foo" class ActorBaseTest(ActorBase): def bar(self): print "bar" abt = ActorBaseTest() # no errors! If I

Inheriting from both ABC and django.db.models.Model raises metaclass exception

三世轮回 提交于 2020-06-16 17:15:07
问题 I am trying to implement a Django data model class, which is also an interface class, using Python 3. My reason for doing so is, I'm writing a base class for my colleague, and need him to implement three methods in all of the classes he derives from mine. I am trying to give him a simplified way to use the functionality of a system I've designed. But, he must override a few methods to supply the system with enough information to execute the code in his inherited classes. I know this is wrong,

Register subclass to an ABC class inside __init_subclass__ does not fully work

。_饼干妹妹 提交于 2020-06-16 07:56:51
问题 What I want to achieve is to register one type as subtype of all other types. For some other reason I cannot use metaclass, so __init_subclass__ seems like a reasonable choice. I have code like this from abc import ABC class AnyData(ABC): pass class BaseData(ABC): def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) cls.register(AnyData) class DataA(BaseData): pass However issubclass(AnyData, DataA) returns False , until the DataA is subclassed, like class DataB(DataA):

我是谁?[C#]

て烟熏妆下的殇ゞ 提交于 2020-03-31 00:15:19
有一个 interface ABC 包括了如下的方法 M(): public interface ABC { void M(); } 另外有个类 Class1 继承了 ABC 并且拥有自己的方法 N(): public class Class1 : ABC { public Class1() {} public void M() { Console.WriteLine( " Now we are in Class1, using method which inherit from Interface ABC " ); } public void N() { Console.WriteLine( " Now we are in Class1, we used method which not inherit from Interface ABC " ); } } 程序中有如下语句: // Code #01 ABC t = new Class1(); t.M(); 这种情况下编译可以通过而且会输出正确的结果。但是如果是下面的情况: // Code #02 ABC t = new Class1(); t.N(); 编译就会出错,所以通过 ABC t = new Class1() 这句话我们得到的肯定不是 Class1 的一个实例。但是接口是不可以实例化的,那么 ABC t = new

python re模块常用的正则表达式

心不动则不痛 提交于 2020-03-11 07:32:43
'.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r "^a" , "\nabc\neee" ,flags = re.MULTILINE) '$' 匹配字符结尾,或e.search( "foo$" , "bfoo\nsdfsf" ,flags = re.MULTILINE).group()也可以 '*' 匹配 * 号前的字符 0 次或多次,re.findall( "ab*" , "cabb3abcbbac" ) 结果为[ 'abb' , 'ab' , 'a' ] '+' 匹配前一个字符 1 次或多次,re.findall( "ab+" , "ab+cd+abb+bba" ) 结果[ 'ab' , 'abb' ] '?' 匹配前一个字符 1 次或 0 次 '{m}' 匹配前一个字符m次 '{n,m}' 匹配前一个字符n到m次,re.findall( "ab{1,3}" , "abb abc abbcbbb" ) 结果 'abb' , 'ab' , 'abb' ] '|' 匹配|左或|右的字符,re.search( "abc|ABC" , "ABCBabcCD" ).group() 结果 'ABC' '(...)' 分组匹配,re.search( "(abc)

Day11面试题答案

谁说胖子不能爱 提交于 2020-02-26 10:22:38
1.判断定义为String类型的s1和s2是否相等 String s1 = “abc”; //常量池中没有这个字符串对象,就创建一个,如果有直接 用即可 String s2 = “abc”; //意思就是 常量池中已经创建了对象s1 的 值abc,s2直接拿来用就行了! System.out.println(s1 == s2); //true System.out.println(s1.equals(s2)); //true 2.下面这句话在内存中创建了几个对象? String s1 = new String("abc"); //创建二个对象,一个在常量池,一个在堆内存 //现在常量池中创建“abc”,然后new String 进堆,将常量池中的“abc”赋给堆。二者的地址值不同。也就是说new的对象是常量池中的abc的副本。 3.判断定义为String类型的s1和s2是否相等 String s1 = new String("abc");· //记录的是堆内存对象的地址值 String s2 = “abc”; //记录的是常量池的地址值 System.out.println(s1 == s2); //s1的abc进入的是堆内存,s2进入的是常量池,结果为false System.out.println(s1.equals(s2)); //equals(