inheritance

Inheritance - __hash__ sets to None in a subclass

雨燕双飞 提交于 2020-05-13 05:05:40
问题 I managed to reproduce this on both Python 3.4 and 3.7. Consider: class Comparable: def _key(self): raise NotImplementedError def __hash__(self): return hash(self._key()) def __eq__(self, other): ... def __lt__(self, other): ... class A(Comparable): pass class B(A): def __str__(self): return "d" def __eq__(self, other): return isinstance(self, type(other)) def _key(self): return str(self), b = B() Clearly one would expect b.__hash__ to be defined here, since it is defined under Comparable

Name of Design Pattern: get class from class level

谁说胖子不能爱 提交于 2020-05-12 20:32:09
问题 Especially in unittests we use this "design pattern" I call "get class from class level" framworktest.py: class FrameWorkHttpClient(object): .... class FrameWorkTestCase(unittest.TestCase): # Subclass can control the class which gets used in get_response() HttpClient=FrameWorkHttpClient def get_response(self, url): client=self.HttpClient() return client.get(url) mytest.py: class MyHttpClient(FrameWorkHttpClient): .... class MyTestCase(FrameWorkTestCase): HttpClient=MyHttpClient def test

Name of Design Pattern: get class from class level

 ̄綄美尐妖づ 提交于 2020-05-12 20:31:29
问题 Especially in unittests we use this "design pattern" I call "get class from class level" framworktest.py: class FrameWorkHttpClient(object): .... class FrameWorkTestCase(unittest.TestCase): # Subclass can control the class which gets used in get_response() HttpClient=FrameWorkHttpClient def get_response(self, url): client=self.HttpClient() return client.get(url) mytest.py: class MyHttpClient(FrameWorkHttpClient): .... class MyTestCase(FrameWorkTestCase): HttpClient=MyHttpClient def test

Why a final class cannot be inherited but a final method can be inherited?

岁酱吖の 提交于 2020-05-10 06:24:39
问题 I have a big confusion in the usage of "final" keyword between classes and methods i.e.why final methods only support inheritance but not final classes final class A{ void print(){System.out.println("hello world");} } class Final extends A{ public static void main(String[] args){ System.out.println("hello world"); } } ERROR: cannot inherit from Final A class Final extennds A{ FINAL METHOD IS.. class Bike{ final void run(){System.out.println("run");} } class Honda extends Bike{ public static

Why a final class cannot be inherited but a final method can be inherited?

泄露秘密 提交于 2020-05-10 06:23:27
问题 I have a big confusion in the usage of "final" keyword between classes and methods i.e.why final methods only support inheritance but not final classes final class A{ void print(){System.out.println("hello world");} } class Final extends A{ public static void main(String[] args){ System.out.println("hello world"); } } ERROR: cannot inherit from Final A class Final extennds A{ FINAL METHOD IS.. class Bike{ final void run(){System.out.println("run");} } class Honda extends Bike{ public static

Calling a parent class constructor from a child class in python [duplicate]

隐身守侯 提交于 2020-05-09 19:04:26
问题 This question already has answers here : Chain-calling parent initialisers in python [duplicate] (3 answers) How to invoke the super constructor in Python? (7 answers) Closed 3 years ago . So if I have a class: class Person(object): '''A class with several methods that revolve around a person's Name and Age.''' def __init__(self, name = 'Jane Doe', year = 2012): '''The default constructor for the Person class.''' self.n = name self.y = year And then this subclass: class Instructor(Person): ''

Force use of a base class method in Java

你离开我真会死。 提交于 2020-05-08 07:17:28
问题 Lets say I have two classes Base and Derived : public class Base { public Base() { } public void methodA() { System.out.println("Base: methodA"); methodB(); } public void methodB() { System.out.println("Base: methodB"); } } public class Derived extends Base { public Derived() { } public void methodA() { super.methodA(); System.out.println("Derived: methodA"); } public void methodB() { System.out.println("Derived: methodB"); } } Now with this: Base d = new Derived(); d.methodA(); Will print:

Force use of a base class method in Java

放肆的年华 提交于 2020-05-08 07:16:32
问题 Lets say I have two classes Base and Derived : public class Base { public Base() { } public void methodA() { System.out.println("Base: methodA"); methodB(); } public void methodB() { System.out.println("Base: methodB"); } } public class Derived extends Base { public Derived() { } public void methodA() { super.methodA(); System.out.println("Derived: methodA"); } public void methodB() { System.out.println("Derived: methodB"); } } Now with this: Base d = new Derived(); d.methodA(); Will print:

Python - why can I call a class method with an instance?

让人想犯罪 __ 提交于 2020-05-08 06:53:07
问题 New to Python and having done some reading, I'm making some methods in my custom class class methods rather than instance methods. So I tested my code but I hadn't changed some of the method calls to call the method in the class rather than the instance, but they still worked: class myClass: @classmethod: def foo(cls): print 'Class method foo called with %s.'%(cls) def bar(self): print 'Instance method bar called with %s.'%(self) myClass.foo() thing = myClass() thing.foo() thing.bar() This

dict attribute 'type' to select Subclass of dataclass

霸气de小男生 提交于 2020-04-30 06:32:09
问题 I have the following class @dataclass_json @dataclass class Source: type: str =None label: str =None path: str = None and the two subclasses: @dataclass_json @dataclass class Csv(Source): csv_path: str=None delimiter: str=';' and @dataclass_json @dataclass class Parquet(Source): parquet_path: str=None Given now the dictionary: parquet={type: 'Parquet', label: 'events', path: '/.../test.parquet', parquet_path: '../../result.parquet'} csv={type: 'Csv', label: 'events', path: '/.../test.csv',