class-method

Ruby Class Methods vs. Methods in Eigenclasses

自古美人都是妖i 提交于 2019-12-04 01:23:37
Are class methods and methods in the eigenclass (or metaclass) of that class just two ways to define one thing? Otherwise, what are the differences? class X # class method def self.a "a" end # eigenclass method class << self def b "b" end end end Do X.a and X.b behave differently in any way? I recognize that I can overwrite or alias class methods by opening the eigenclass: irb(main):031:0> class X; def self.a; "a"; end; end => nil irb(main):032:0> class X; class << self; alias_method :b, :a; end; end => #<Class:X> irb(main):033:0> X.a => "a" irb(main):034:0> X.b => "a" irb(main):035:0> class X

Using same function as instance and classmethod in python

别说谁变了你拦得住时间么 提交于 2019-12-03 16:05:30
One can do something like this: class master: @combomethod def foo(param): param.bar() # Param could be type as well as object class slaveClass( master ): @classmethod def bar(cls): print("This is class method") slaveType = slaveClass slaveType.foo() class slaveInstance( master ): def __init__(self, data): self.data = data def bar(self): print("This is "+self.data+" method") slaveType = slaveInstance("instance") slaveType.foo() combomethod is defined in " Creating a method that is simultaneously an instance and class method ". My question is, why is it like this, that default first parameter

Objective C - Calling a class method on the main thread?

谁说胖子不能爱 提交于 2019-12-03 13:03:51
How can I call a CLASS METHOD on the main thread? Something like: [SomeClass performSelectorOnMainThread:staticMethod withObject:nil]; Please don't tell me to create a regular method to call this class method. That would be an obvious solution, but not clean. Thanks [SomeClass performSelectorOnMainThread:staticMethod withObject:nil waitUntilDone:NO]; Yes, performSelectorOnMainThread:withObject:waitUntilDone: is not a class method. Yes, it is an instance method on NSObject . Yes, all Class objects are instances of NSObject . ( I'm not kidding! ) You could also do: dispatch_async(dispatch_get

Calling class methods via class name vs self

淺唱寂寞╮ 提交于 2019-12-03 11:21:30
问题 Suppose we have a class named Calculator . There's a class method in it, called runProgram . If I wanted to call this class method, inside the class's implementation, what would the difference between these two be: [Calculator runProgram] OR [self runProgram] Are these both the same? 回答1: If inside an instance method: [self runProgram] in this, self means the object instance itself, and thus it will generate a runtime error. You want to use [[self class] runProgram] instead. However, if you

Why use classmethod instead of staticmethod? [duplicate]

心已入冬 提交于 2019-12-03 10:45:19
问题 This question already has answers here : Difference between staticmethod and classmethod (25 answers) Closed 6 years ago . I know what they do and I've seen many examples of both, but I haven't found a single example where I would have to use classmethod instead of replacing it with a staticmethod . The most common example of classmethod I've seen is for creating a new instance of the class itself, like this (very simplified example, there's no use of the method atm. but you get the idea):

Cannot call a class method with [self theMethod:]

▼魔方 西西 提交于 2019-12-03 05:24:00
I am trying to write a class method in Objective C. The project builds fine when I declare the method. But the build fails whenever I try to call the method. Here is my code. Header File #import <UIKit/UIKit.h> @interface LoginViewController : UIViewController { //Declare Vars } - (IBAction) login: (id) sender; + (NSString *) md5Hash:(NSString *)str; @end Source File + (NSString *) md5Hash:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5( cStr, strlen(cStr), result ); return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X

Check if a function uses @classmethod

萝らか妹 提交于 2019-12-03 03:19:15
TL;DR How do I find out whether a function was defined using @classmethod or something with the same effect? My problem For implementing a class decorator I would like to check if a method takes the class as its first argument, for example as achieved via @classmethod def function(cls, ...): I found a solution to check for @staticmethod via the types module ( isinstance(foo, types.UnboundMethodType) is False if the foo is static, see here ), but did not find anything on how to do so for @classmethod Context What I am trying to do is something along the lines of def class_decorator(cls): for

Calling class methods via class name vs self

别说谁变了你拦得住时间么 提交于 2019-12-03 02:50:28
Suppose we have a class named Calculator . There's a class method in it, called runProgram . If I wanted to call this class method, inside the class's implementation, what would the difference between these two be: [Calculator runProgram] OR [self runProgram] Are these both the same? If inside an instance method: [self runProgram] in this, self means the object instance itself, and thus it will generate a runtime error. You want to use [[self class] runProgram] instead. However, if you call this method from another class method, then [self runProgram] is correct, since now self refers to the

Why use classmethod instead of staticmethod? [duplicate]

对着背影说爱祢 提交于 2019-12-03 01:16:26
This question already has answers here : What is the difference between @staticmethod and @classmethod? (23 answers) I know what they do and I've seen many examples of both, but I haven't found a single example where I would have to use classmethod instead of replacing it with a staticmethod . The most common example of classmethod I've seen is for creating a new instance of the class itself, like this (very simplified example, there's no use of the method atm. but you get the idea): class Foo: @classmethod def create_new(cls): return cls() This would return a new instance of Foo when calling

Ruby: Calling class method from instance

你。 提交于 2019-12-02 13:49:02
In Ruby, how do you call a class method from one of that class's instances? Say I have class Truck def self.default_make # Class method. "mac" end def initialize # Instance method. Truck.default_make # gets the default via the class's method. # But: I wish to avoid mentioning Truck. Seems I'm repeating myself. end end the line Truck.default_make retrieves the default. But is there a way of saying this without mentioning Truck ? It seems like there should be. Rather than referring to the literal name of the class, inside an instance method you can just call self.class.whatever . class Foo def