class-method

Ruby Class Methods vs. Methods in Eigenclasses

假如想象 提交于 2019-12-21 07:11:53
问题 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;

Cannot call a class method with [self theMethod:]

烈酒焚心 提交于 2019-12-20 16:24:22
问题 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

Python noob can't get class method to work

烈酒焚心 提交于 2019-12-20 04:15:29
问题 I have a method in my Customer class called save_from_row() . It looks like this: @classmethod def save_from_row(row): c = Customer() c.name = row.value('customer', 'name') c.customer_number = row.value('customer', 'number') c.social_security_number = row.value('customer', 'social_security_number') c.phone = row.value('customer', 'phone') c.save() return c When I try to run my script, I get this: Traceback (most recent call last): File "./import.py", line 16, in <module> Customer.save_from

Duck typing and class methods (or, how to use a method from both a class and an instance?)

折月煮酒 提交于 2019-12-19 04:31:11
问题 I have some code which I would like to pass instances or classes interchangeably. All I will do in that code is to call a method that I expect both classes and instances to have (the method go() in the example below). Unfortunately, I can't create a classmethod with the same name of a regular method... See example below. I initially expected the second call to produce an a instead of a b . Any advice on how to achieve this? Type "help", "copyright", "credits" or "license" for more information

How does assignment of a function as a class attribute become a method in Python?

不想你离开。 提交于 2019-12-18 02:47:03
问题 >>> class A(object): pass >>> def func(cls): pass >>> A.func = func >>> A.func <unbound method A.func> How does this assignment create a method? It seems unintuitive that assignment does the following for classes: Turn functions into unbound instance methods Turn functions wrapped in classmethod() into class methods (actually, this is pretty intuitive) Turn functions wrapped in staticmethod() into functions It seems that for the first, there should be an instancemethod() , and for the last

How do I list all objects created from a class in Ruby? [duplicate]

*爱你&永不变心* 提交于 2019-12-17 22:36:08
问题 This question already has answers here : How to find each instance of a class in Ruby (2 answers) Closed 4 years ago . Is there any way in Ruby for a class to know how many instances of it exist and can it list them? Here is a sample class: class Project attr_accessor :name, :tasks def initialize(options) @name = options[:name] @tasks = options[:tasks] end def self.all # return listing of project objects end def self.count # return a count of existing projects end end Now I create project

ARC Semantic Issue: No visible @interface for Class declares the selector

谁说胖子不能爱 提交于 2019-12-17 20:13:29
问题 Pretty basic stuff but i am unable to troubleshoot where the problem is. In my project, i have a class named "TheFeedStore" with following two methods: - (BOOL)hasItemBeenRead:(RSSItem *)item { ............ } - (void)markItemAsRead:(RSSItem *)item { ......... } I am using the following class method so other classes can access these methods using it: + (TheFeedStore *) sharedStore { static TheFeedStore *feedStore = nil; if (!feedStore) { feedStore = [[TheFeedStore alloc] init]; } return

why __getitem__ cannot be classmethod?

岁酱吖の 提交于 2019-12-17 19:38:01
问题 Suppose following class: class Class(object): @classmethod def getitem(*args): print 'getitem %s' % (args,) @classmethod def __getitem__(*args): print '__getitem__ %s' % (args,) The getitem method behaves as expected: it receives Class as first arg, but __getitem__ receives type as first arg: calling Class.getitem(test) getitem (<class '__main__.Class'>, 'test') calling obj.getitem(test) getitem (<class '__main__.Class'>, 'test') calling Class[test] 'type' object has no attribute '__getitem__

Factory method for objects - best practice?

我的梦境 提交于 2019-12-17 18:05:10
问题 This is a question regarding the best practice for creating an instance of a class or type from different forms of the same data using python. Is it better to use a class method or is it better to use a separate function altogether? Let's say I have a class used to describe the size of a document. (Note: This is simply an example. I want to know the best way to create an instance of the class not the best way to describe the size of a document.) class Size(object): """ Utility object used to

Python dynamic class methods

一笑奈何 提交于 2019-12-17 16:25:10
问题 Say there is: class A(B): ... where B could be object and ... is not : @classmethod # or @staticmethod def c(cls): print 'Hello from c!' What do I have to do that calling A.c() wont trigger AttributeError ? In other words, I know it is possible to manually add class methods to a class at runtime. But is it possible to do so automatically, say every time a class method is missing it creates some dummy method? In yet another words, only if I could replace A.__dict__ with my dict which handles _