subclass

Subclassing int in Python

眉间皱痕 提交于 2019-11-26 07:37:58
问题 I\'m interested in subclassing the built-in int type in Python (I\'m using v. 2.5), but having some trouble getting the initialization working. Here\'s some example code, which should be fairly obvious. class TestClass(int): def __init__(self): int.__init__(self, 5) However, when I try to use this I get: >>> a = TestClass() >>> a 0 where I\'d expect the result to be 5 . What am I doing wrong? Google, so far, hasn\'t been very helpful, but I\'m not really sure what I should be searching for

When do you need to explicitly call a superclass constructor?

大兔子大兔子 提交于 2019-11-26 07:24:52
问题 So say I have a subclass that extends a superclass. In what scenarios do I need to explicitly type super() to get the superclass constructor to run? I\'m looking at an example in a book about abstract classes and when they extend it with a non-abstract subclass, the subclass\'s default constructor is blank and there\'s a comment that says the superclass\'s default constructor will be called. At the same time I\'ve also seen instances on here where someone\'s problem was not explicitly calling

Why do I get “non-static variable this cannot be referenced from a static context”?

半城伤御伤魂 提交于 2019-11-26 06:07:56
问题 I have a very simple class which I want to use as a subclass of another one. But when I put its code in the parent\'s class I get : non-static variable this cannot be referenced from a static context On the other hand when I put the sublass GenTest \'s class code outside the the \"parent\'s\" class code - JavaApp1 I do not get this error. public class JavaApp1 { class GenTest { @Deprecated void oldFunction() { System.out.println(\"don\'t use that\"); } void newFunction() { System.out.println(

How to find all the subclasses of a class given its name?

ⅰ亾dé卋堺 提交于 2019-11-26 05:42:32
问题 I need a working approach of getting all classes that are inherited from a base class in Python. 回答1: New-style classes (i.e. subclassed from object , which is the default in Python 3) have a __subclasses__ method which returns the subclasses: class Foo(object): pass class Bar(Foo): pass class Baz(Foo): pass class Bing(Bar): pass Here are the names of the subclasses: print([cls.__name__ for cls in Foo.__subclasses__()]) # ['Bar', 'Baz'] Here are the subclasses themselves: print(Foo._

Why aren't superclass __init__ methods automatically invoked?

感情迁移 提交于 2019-11-26 04:59:07
问题 Why did the Python designers decide that subclasses\' __init__() methods don\'t automatically call the __init__() methods of their superclasses, as in some other languages? Is the Pythonic and recommended idiom really like the following? class Superclass(object): def __init__(self): print \'Do something\' class Subclass(Superclass): def __init__(self): super(Subclass, self).__init__() print \'Do something else\' 回答1: The crucial distinction between Python's __init__ and those other languages

Abstract classes in Swift Language

爱⌒轻易说出口 提交于 2019-11-26 04:38:34
问题 Is there a way to create an abstract class in the Swift Language, or is this a limitation just like Objective-C? I\'d like to create a abstract class comparable to what Java defines as an abstract class. 回答1: There are no abstract classes in Swift (just like Objective-C). Your best bet is going to be to use a Protocol, which is like a Java Interface. With Swift 2.0, you can then add method implementations and calculated property implementations using protocol extensions. Your only

How do you use the ellipsis slicing syntax in Python?

妖精的绣舞 提交于 2019-11-26 04:36:03
问题 This came up in Hidden features of Python, but I can\'t see good documentation or examples that explain how the feature works. 回答1: You'd use it in your own class, since no builtin class makes use of it. Numpy uses it, as stated in the documentation. Some examples here. In your own class, you'd use it like this: >>> class TestEllipsis(object): ... def __getitem__(self, item): ... if item is Ellipsis: ... return "Returning all items" ... else: ... return "return %r items" % item ... >>> x =

Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?

梦想与她 提交于 2019-11-26 03:57:54
问题 I have a question about inheritance in Java. I have two classes A and B , and class B, inherits from A: public class A { public A() { System.out.println(\"Hi!\"); } } public class B extends A { public B() { System.out.println(\"Bye!\"); } public static void main(String[] args) { B b = new B(); } } When I run program B, the output is: Hi! Bye! Question : why the constructor of class A is invoked, when I create and object of class B ? I know that B inherits everything from A - all instance or

Subclassing tuple with multiple __init__ arguments

与世无争的帅哥 提交于 2019-11-26 03:28:31
问题 The following code works: class Foo(tuple): def __init__(self, b): super(Foo, self).__init__(tuple(b)) if __name__ == \'__main__\': print Foo([3, 4]) $ python play.py Result: play.py:4: DeprecationWarning: object.__init__() takes no parameters super(Foo, self).__init__(tuple(b)) (3, 4) But not the following: class Foo(tuple): def __init__(self, a, b): super(Foo, self).__init__(tuple(b)) if __name__ == \'__main__\': print Foo(None, [3, 4]) $ python play.py Result: Traceback (most recent call

Is it possible to call subclasses' methods on a superclass object?

不问归期 提交于 2019-11-26 02:58:13
问题 Animal is a superclass of Dog and Dog has a method called bark public void bark() { System.out.println(\"woof\"); } Consider the following: Animal a = new Dog(); if (a instanceof Dog){ a.bark(); } What will happen? the assignment isn\'t allowed the call to bark is allowed and \"woof\" is printed at run time the call to bark is allowed but nothing is printed the call to bark causes a compile time error the call to bark results in a run time error I said 2 as we are checking if the object is a