class-method

Static classes in Python

半城伤御伤魂 提交于 2019-11-30 00:17:56
问题 I once read (I think on a page from Microsoft) that it's a good way to use static classes, when you don't NEED two or more instances of a class. I'm writing a program in Python. Is it a bad style, if I use @classmethod for every method of a class? 回答1: In my experience creating a class is a very good solution for a number of reasons. One is that you wind up using the class as a 'normal' class (esp. making more than just one instance) more often than you might think. It's also a reasonable

Using super with a class method

試著忘記壹切 提交于 2019-11-29 22:46:14
I'm trying to learn the super() function in Python. I thought I had a grasp of it until I came over this example (2.6) and found myself stuck. http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html#super-with-classmethod-example Traceback (most recent call last): File "<stdin>", line 1, in <module> File "test.py", line 9, in do_something do_something = classmethod(do_something) TypeError: unbound method do_something() must be called with B instance as first argument (got nothing instead) >>> It wasn't what I expected when I read this line right before

Convert a BaseClass object into a SubClass object idiomatically?

时光怂恿深爱的人放手 提交于 2019-11-29 18:45:35
问题 There is a base class Base and a subclass Special . class Base(object): def __init__(self, name): self.name = name def greet(self): return 'Hello %s' % self.name class Special(Base): def __init__(self, name): super(Special, self).__init__(name) def rhyme(self): return 'Hi %s! How are you? Fine, thanks. What about you?' % self.name How can I turn an instance of Base into an instance Special ? Currently I have a classmethod defined on Special that just reassignes __dict__ : class Special(Base):

Using class/static methods as default parameter values within methods of the same class

时光怂恿深爱的人放手 提交于 2019-11-29 15:09:43
问题 I'd like to do something like this: class SillyWalk(object): @staticmethod def is_silly_enough(walk): return (False, "It's never silly enough") def walk(self, appraisal_method=is_silly_enough): self.do_stuff() (was_good_enough, reason) = appraisal_method(self) if not was_good_enough: self.execute_self_modifying_code(reason) return appraisal_method def do_stuff(self): pass def execute_self_modifying_code(self, problem): from __future__ import deepjuju deepjuju.kiss_booboo_better(self, problem)

Is there any way to create a class property in Python?

旧巷老猫 提交于 2019-11-29 14:15:04
问题 The following doesn't work for some reason: >>> class foo(object): ... @property ... @classmethod ... def bar(cls): ... return "asdf" ... >>> foo.bar <property object at 0x1da8d0> >>> foo.bar + '\n' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'property' and 'str' Is there a way to do this, or is my only alternative to resort to some kind of metaclass trickery? 回答1: If you want the descriptor property to trigger when you

Objective C Class Methods vs C Functions

女生的网名这么多〃 提交于 2019-11-29 13:32:18
问题 While working on on open source project, I came across the following C function declaration and implementation: // FSNData.h NSString *stringForMimeType(MimeType type); @interface FSNData : NSObject // All the expected objective-c property and instance method declarations @end // FSNData.m #import "FSNData.h" // where 'type' is an enum // this does work as expected NSString *stringForMimeType(MimeType type) { switch (type) { case MimeType_image_jpeg: return @"image/jpeg"; case MimeType_image

Same name for classmethod and instancemethod?

ぃ、小莉子 提交于 2019-11-29 09:54:47
I'd like to do something like this: class X: @classmethod def id(cls): return cls.__name__ def id(self): return self.__class__.__name__ And now call id() for either the class or an instance of it: >>> X.id() 'X' >>> X().id() 'X' Obviously this exact code doesn't work, but is there a similar way to make it work? Or any other workarounds to get such behavior without too much "hacky" stuff? Class and instance methods live in the same namespace and you cannot reuse names like that; the last definition of id will win in that case. The class method will continue to work on instances however, there

How does a classmethod object work?

耗尽温柔 提交于 2019-11-29 09:23:05
问题 I'm having trouble to understand how a classmethod object works in Python, especially in the context of metaclasses and in __new__ . In my special case I would like to get the name of a classmethod member, when I iterate through the members that were given to __new__ . For normal methods the name is simply stored in a __name__ attribute, but for a classmethod there is apparently no such attribute. I don't even see how the classmethod is invoked, as there is no __call__ attribute either. Can

How do I return a struct value from a runtime-defined class method under ARC?

本秂侑毒 提交于 2019-11-29 04:23:24
I have a class method returning a CGSize and I'd like to call it via the Objective-C runtime functions because I'm given the class and method names as string values. I'm compiling with ARC flags in XCode 4.2. Method signature: +(CGSize)contentSize:(NSString *)text; The first thing I tried was to invoke it with objc_msgSend like this: Class clazz = NSClassFromString(@"someClassName); SEL method = NSSelectorFromString(@"contentSize:"); id result = objc_msgSend(clazz, method, text); This crashed with "EXC_BAD_ACCESS" and without a stack trace. I used this first because the documentation for objc

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

心不动则不痛 提交于 2019-11-28 23:43:24
>>> 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 one, there shouldn't be a wrapper function at all. I understand that these are for uses within a class