subclass

python subclass access to class variable of parent

北城余情 提交于 2019-11-27 07:13:52
I was surprised to to learn that a class variable of a subclass can't access a class variable of the parent without specifically indicating the class name of the parent: >>> class A(object): ... x = 0 ... >>> class B(A): ... y = x+1 ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in B NameError: name 'x' is not defined >>> class B(A): ... y = A.x + 1 ... >>> B.x 0 >>> B.y 1 Why is it that in defining B.y I have to refer to A.x and not just x? This is counter to my intuition from instance variables, and since I can refer to B.x after B is

How do I check (at runtime) if one class is a subclass of another?

[亡魂溺海] 提交于 2019-11-27 06:46:27
Let's say that I have a class Suit and four subclasses of suit: Heart, Spade, Diamond, Club. class Suit: ... class Heart(Suit): ... class Spade(Suit): ... class Diamond(Suit): ... class Club(Suit): ... I have a method which receives a suit as a parameter, which is a class object, not an instance. More precisely, it may receive only one of the four values: Heart, Spade, Diamond, Club. How can I make an assertion which ensures such a thing? Something like: def my_method(suit): assert(suit subclass of Suit) ... I'm using Python 3. You can use issubclass() like this assert issubclass(suit, Suit) .

how to set UIButton type in UIButton Subclass

青春壹個敷衍的年華 提交于 2019-11-27 06:45:07
问题 I am subclassing the UIButton, what i want is to set the button type to Round Rect. Button.h @interface Button : UIButton {} - (void)initialize; @end Button.m @implementation Button - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initialize]; } return self; } -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if(self){ [self initialize]; } return self; } - (void)initialize { self.titleLabel.font = [UIFont systemFontOfSize

Subclass in type hinting

房东的猫 提交于 2019-11-27 06:35:58
问题 I want to allow type hinting using Python 3 to accept sub classes of a certain class. E.g.: class A: pass class B(A): pass class C(A): pass def process_any_subclass_type_of_A(cls: A): if cls == B: # do something elif cls == C: # do something else Now when typing the following code: process_any_subclass_type_of_A(B) I get an PyCharm IDE hint 'Expected type A, got Type[B] instead.' How can I change type hinting here to accept any subtypes of A? According to this (https://www.python.org/dev/peps

Make Background of UIView a Gradient Without Sub Classing

痴心易碎 提交于 2019-11-27 06:23:41
Is there a way to make the background of a UIView a gradient without subclassing it? I'd rather not use an image file to accomplish this either. It just seems obtuse to have to subclass UIView just to draw a gradient for the background. rpetrich You can use +[UIColor colorWithPatternImage:] to produce a patterned background. Example (bring your own CGGradient): // Allocate color space CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); // Allocate bitmap context CGContextRef bitmapContext = CGBitmapContextCreate(NULL, 320, 480, 8, 4 * 320, colorSpace, kCGImageAlphaNoneSkipFirst); /

UIBezierPath Subclass Initializer

强颜欢笑 提交于 2019-11-27 06:22:34
问题 I'm trying to create a subclass of UIBezierPath to add some properties that are useful to me. class MyUIBezierPath : UIBezierPath { var selectedForLazo : Bool! = false override init(){ super.init() } /* Compile Error: Must call a designated initializer of the superclass 'UIBezierPath' */ init(rect: CGRect){ super.init(rect: rect) } /* Compile Error: Must call a designated initializer of the superclass 'UIBezierPath' */ init(roundedRect: CGRect, cornerRadius: CGFloat) { super.init(roundedRect:

Win32 custom message box

只愿长相守 提交于 2019-11-27 06:08:12
问题 I want to make a custom message box. What I want to customize is the button's text. MessageBoxW( NULL, L"Target folder already exists. Do you want to overwrite the folder?", L"No title", MB_YESNOCANCEL | MB_ICONQUESTION ); I'd like to just change the buttons text to Overwrite , Skip , Cancel . What's the most simple way? I have to make this as having same look and feel with Windows default messagebox. 回答1: As said by others, a typical way is to create a dialog resource and have a completely

Rotating a view in layoutSubviews

有些话、适合烂在心里 提交于 2019-11-27 05:29:06
Background and goal Goal: I want to rotate and flip a UITextView . ( Why: see my previous question ) Problem: If I do the transform directly on the UITextView , the text layout gets messed up for some unknown reason. Solution: Put the UITextView in a UIView container, and then do the transform on the container. New problem: Auto Layout (or any sort of layout) on the rotated view becomes a major headache . Proposed solution: Make a subclass of UIView that acts as an additional container for the rotated and flipped UIView . Auto Layout should then work on this custom view. Current Problem It

Delphi subclass visual component and use it

可紊 提交于 2019-11-27 04:43:13
问题 I would like to subclass TToolBar with another class called MyTToolBar so that I can override a method. I'm new to Delphi, but after two hours of trying various methods, I can't get MyTToolBar to be used instead of TToolBar. I can't be the first person who's wanted to override a method on a visual component class. I come more from an Xcode background where subclassing a visual component is easy. You create a subclass (e.g., MySubclass) of a parent class (e.g., MySuperClass) and then just

Is calling super in a category the same as calling it in a subclass?

最后都变了- 提交于 2019-11-27 04:39:46
Does calling [super init] do the same thing in a category as a subclass? If not, what's the difference? In order to understand this, it's probably important to understand the way an object is stored during runtime. There is a class object 1 , which holds all the method implementations, and separately, there is a structure with the storage for the instance's variables. All instances of a class share the one class object. When you call a method on an instance, the compiler turns that into a call to objc_msgSend ; the method implementation is looked up in the class object, and then run with the