class-method

Classmethods in Generic Protocols with self-types, mypy type checking failure

≯℡__Kan透↙ 提交于 2019-12-07 00:22:03
问题 A little background, I essentially need to define an int wrapper type, say MyInt (among some other classes), and another generic Interval type which can accept MyInt objects as well as other types of objects. Since the types acceptable by the Interval do not fall into a neat hierarchy, I thought this would be a perfect use-case for the experimental Protocol , which in my case would require a couple of methods and a couple of @classmethod s. All the methods return a "self-type", i.e., MyInt.my

Python set docstring and get method name of dynamically generated classmethod

安稳与你 提交于 2019-12-06 20:35:28
I'm trying to get/set the name and docstring of dynamically created class methods as follows, but am having trouble figuring out exactly how to do it: import sys import inspect class test(object): pass @classmethod def genericFunc(cls, **kwargs): print "function:", (inspect.stack()[0][3]) print "kwargs:", kwargs function_list = ['myF1', 'myF2'] for func in function_list: setattr(test, func, genericFunc) #set docstring for func here? if __name__ == '__main__': x = test() print "docstring:", x.myF1.__doc__ x.myF1(arg1="foo") y = test() print "docstring:", y.myF2.__doc__ y.myF2(arg1="foo", arg2=

Objective-C: What does [ClassName self]; do?

跟風遠走 提交于 2019-12-06 20:17:38
问题 I'm looking through the source code for the CocoaHTTPServer project, more specifically the HTTPServer.m file and I just don't understand this line: connectionClass = [HTTPConnection self]; What does this do (is it documented anywhere)? How does it even compile? Should it not be connectionClass = [HTTPConnection class]; 回答1: In this context, - (id)self is a method defined on NSObject . It returns the receiver. For a Class it should obviously do the same as a call to the -(Class)class . Class

Class methods (ruby)

北城以北 提交于 2019-12-06 19:38:27
Newbie here, having a hard time understanding Class methods and why I cannot get an attribute to show up correctly in the instance. class Animal attr_accessor :noise, :color, :legs, :arms def self.create_with_attributes(noise, color) animal = self.new(noise) @noise = noise @color = color return animal end def initialize(noise, legs=4, arms=0) @noise = noise @legs = legs @arms = arms puts "----A new animal has been instantiated.----" end end animal1 = Animal.new("Moo!", 4, 0) puts animal1.noise animal1.color = "black" puts animal1.color puts animal1.legs puts animal1.arms puts animal2 = Animal

How do I use the python-decorator package to decorate classmethods?

别来无恙 提交于 2019-12-06 08:44:25
问题 I'm have a decorator that I want to use to decorate class methods. In the following example, the @mydec decorator works fine on its own, however it does not preserve the function signature when using help() or pydoc. In order to fix this, I looked at using @decorator python-decorator package: import functools import decorator @decorator.decorator def mydec(func): @functools.wraps(func) def inner(cls, *args, **kwargs): # do some stuff return func(cls, *args, **kwargs) return inner class Foo

Objective C & OC Mock - Mocking a class method?

允我心安 提交于 2019-12-06 04:23:04
问题 I need to be able to determine whether a class method was called or not. How can I do this with OCMock? 回答1: Starting with OCMock release 2.1 this is supported out of the box. You can now stub class methods in the same way as you stub instance methods. 回答2: One approach is to wrap the class method in a method on your own class. So let's say your class has to call [SomeOtherClass classMethod:someString] . You could create a method invokeClassMethod: on your class like this: -(NSString *

Python: Assigning staticmethod to class variable gives error

∥☆過路亽.° 提交于 2019-12-06 03:55:01
I want to assign a static method to a class variable in Python, and below is what my code looks like. class Klass: classVariable = None @staticmethod def method(): print "method called" Klass.classVariable = Klass.method Klass.method() Klass.classVariable() This gave me an error at the last line, TypeError: unbound method method() must be called with Klass instance as first argument (got nothing instead). But when I change the static method to class method it works. Can anyone give me any idea of why this is the case? Backstory (descriptor protocol) First, we need to know a little about python

How to use delegation (or pass messages) from class methods

ぃ、小莉子 提交于 2019-12-06 00:07:42
I have a class method that gets called by a view controller. I want the view controller to be aware of when the class method has finished its tasks (it has threads on it). I think I should use delegation, but I need an id delegate , and I can't call it by self.delegate , because there is no self in a class method. How should I do this? Thanks! You can store a delegate at class-level (even separate from an object-level delegate), but it sounds a bit fishy to me. Here's how you'd do it: In your header file: @interface SomeClass : SomeBaseClass { ... } ... + (id<SomeDelegateProtocol>

Why is “self = [[Rectangle alloc] init]” in a class method BAD?

徘徊边缘 提交于 2019-12-05 21:28:18
问题 In the document "Objective-C Programming Language" by Apple, page 48 says: + (Rectangle *)rectangleOfColor:(NSColor *) color { self = [[Rectangle alloc] init]; // BAD [self setColor:color]; return self; } + (id)rectangleOfColor:(NSColor *)color { id newInstance = [[Rectangle alloc] init]; // GOOD [newInstance setColor:color]; return newInstance; } + (id)rectangleOfColor:(NSColor *)color { id newInstance = [[self alloc] init]; // EXCELLENT [newInstance setColor:color]; return newInstance; }

Access property from a class method?

痞子三分冷 提交于 2019-12-05 21:16:51
In order to make my code testable, I have created a lazy initializer; this way in my unit test, I can mock any object I want before the getter gets called. When it comes to class methods, though, my class method doesn't have access to the properties I have defined. Is there any way to make the properties accessible by my class method? If not, is there any way to create static variables that are also accessible outside of this class, i.e., accessible by my unit test class? @implementation @synthesize webService; + (void)doSomething { self.webService.url = @"some url"; [self.webService start]; /