subclass

How to subclass an OrderedDict?

蹲街弑〆低调 提交于 2019-12-03 04:23:07
Subclassing a Python dict works as expected: >>> class DictSub(dict): ... def __init__(self): ... self[1] = 10 ... >>> DictSub() {1: 10} However, doing the same thing with a collections.OrderedDict does not work: >>> import collections >>> class OrdDictSub(collections.OrderedDict): ... def __init__(self): ... self[1] = 10 ... >>> OrdDictSub() (…) AttributeError: 'OrdDictSub' object has no attribute '_OrderedDict__root' Thus, the OrderedDict implementation uses a private __root atribute, which prevents the subclass OrdDictSub from behaving like the DictSub subclass. Why? How can one inherit

best way to implement custom pretty-printers

自作多情 提交于 2019-12-03 01:27:21
Customizing pprint.PrettyPrinter The documentation for the pprint module mentions that the method PrettyPrinter.format is intended to make it possible to customize formatting. I gather that it's possible to override this method in a subclass, but this doesn't seem to provide a way to have the base class methods apply line wrapping and indentation. Am I missing something here? Is there a better way to do this (e.g. another module)? Alternatives? I've checked out the pretty module, which looks interesting, but doesn't seem to provide a way to customize formatting of classes from other modules

UILabel subclass initialize with custom color

久未见 提交于 2019-12-03 01:07:48
My goal is to set the textColor of my custom UILabel subclass in my view controller. I have a UILabel subclass named CircleLabel . Here are the basics of it: class CircleLabel: UILabel { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } override init(frame: CGRect) { super.init(frame: frame) } override func drawRect(rect: CGRect) { self.layer.cornerRadius = self.bounds.width/2 self.clipsToBounds = true super.drawRect(rect) } override func drawTextInRect(rect: CGRect) { self.textColor = UIColor.whiteColor() super.drawTextInRect(rect) } func setProperties(borderWidth: Float

Test whether a Ruby class is a subclass of another class

狂风中的少年 提交于 2019-12-03 00:27:24
问题 I would like to test whether a class inherits from another class, but there doesn't seem to exist a method for that. class A end class B < A end B.is_a? A => false B.superclass == A => true A trivial implementation of what I want would be: class Class def is_subclass_of?(clazz) return true if superclass == clazz return false if self == Object superclass.is_subclass_of?(clazz) end end but I would expect this to exist already. 回答1: Just use the < operator B < A # => true A < A # => false or use

Swift subclassing - how to override Init()

不打扰是莪最后的温柔 提交于 2019-12-02 23:20:33
I have the following class, with an init method: class user { var name:String var address:String init(nm: String, ad: String) { name = nm address = ad } } I'm trying to subclass this class but I keep getting errors on the super.init() part: class registeredUser : user { var numberPriorVisits: Int // This is where things start to go wrong - as soon as I type 'init' it // wants to autocomplete it for me with all of the superclass' arguments, // and I'm not sure if those should go in there or not: init(nm: String, ad: String) { // And here I get errors: super.init(nm: String, ad: String) // etc..

Test whether a Ruby class is a subclass of another class

谁说我不能喝 提交于 2019-12-02 14:05:21
I would like to test whether a class inherits from another class, but there doesn't seem to exist a method for that. class A end class B < A end B.is_a? A => false B.superclass == A => true A trivial implementation of what I want would be: class Class def is_subclass_of?(clazz) return true if superclass == clazz return false if self == Object superclass.is_subclass_of?(clazz) end end but I would expect this to exist already. Marcel Jackwerth Just use the < operator B < A # => true A < A # => false or use the <= operator B <= A # => true A <= A # => true Also available: B.ancestors.include? A

How to access Abstract superclass instance variable

白昼怎懂夜的黑 提交于 2019-12-02 13:12:01
So I have two classes: Property and Houses . Property is the abstract super class and Houses is its subclass. Here is the code for Property public abstract class Property{ String pCode; double value; int year; public Property(String pCode, double value , int year){ this.pCode = pCode; this.value = value; this.year = year; } public Property(){ pCode = ""; value = 0; year = 0; } public abstract void depreciation(); //Accessors private String getCode(){ return pCode; } private double getValue(){ return value; } private int getYear(){ return year; } //Mutators private void setCode(String newCode){

python: dynamically adding attributes to a built-in class

纵饮孤独 提交于 2019-12-02 12:24:32
Why doesn't it work for the built-in classes? Is using a subclass the best approach to fix it, or will I run into some hidden problems? a = {} a.p = 1 # raises AttributeError class B(dict): pass b = B() b.p = 1 # works EDIT: my original comment that it doesn't work for b was incorrect (I made a mistake). The builtin classes do not have the ability to have arbitrary attributes. This is done for reasons of performance, especially memory usage, you want the built-in classes like list and dict to be as small as possible so you can have many of them. Therefore the built-in classes do not have the _

iOS Parse PFObject Subclassing with Swift Behaviour

别等时光非礼了梦想. 提交于 2019-12-02 09:54:15
I have a strange issue, my subclassing works in all my view controllers except one. I have imported everything that is required, but this one class is giving me the "Missing argument for parameter 'className' in call" My subclass import Foundation import Parse class Session : PFObject, PFSubclassing { class func parseClassName() -> String { return "Session" } } // i have removed the non important info I perform the Session.registerSubclass() In the app delegate In EVERY class I have i can create a session via var se = Session() But in this one class, this will not work. The class import UIKit

Declare all instances of a typeclass are in another typeclass without modifying the original class declarations

偶尔善良 提交于 2019-12-02 09:24:51
问题 There is an Crypto.Random API inside the crypto-api package that specifies what it means for something to be a "pseudorandom number generator". I have implemented this API using an instance of System.Random's RandomGen class, namely, StdGen: instance CryptoRandomGen StdGen where newGen bs = Right $ mkStdGen $ shift e1 24 + shift e2 16 + shift e3 8 + e4 where (e1 : e2 : e3 : e4 : _) = Prelude.map fromIntegral $ unpack bs genSeedLength = Tagged 4 genBytes n g = Right $ genBytesHelper n empty g