class-method

What's the difference between class methods and instance methods in Swift?

只谈情不闲聊 提交于 2019-11-30 20:55:39
protocol NoteProtocol { var body: NSString? { get set } var createdAt: NSDate? { get set } var entityId: NSString? { get set } var modifiedAt: NSDate? { get set } var title: NSString? { get set } // class methods class func insertNewNoteInManagedObjectContext(managedObjectContext: NSManagedObjectContext!) -> NoteProtocol class func noteFromNoteEntity(noteEntity: NSManagedObject) -> NoteProtocol // instance methods func update(#title: String, body: String) func deleteInManagedObjectContext(managedObjectContext: NSManagedObjectContext!) } Hi This is a piece of code I found on GitHub. In this

Python: How to call an instance method from a class method of the same class

扶醉桌前 提交于 2019-11-30 18:39:52
I have a class as follows: class MyClass(object): int = None def __init__(self, *args, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) def get_params(self): return {'int': random.randint(0, 10)} @classmethod def new(cls): params = cls.get_params() return cls(**params) and I would like to be able to do: >>> obj = MyClass.new() >>> obj.int # must be defined 9 I mean without creating a new instance of MyClass , but obviously it's not that simple, because calling MyClass.new() throws TypeError: unbound method get_params() must be called with MyClass instance as first argument (got

Is it bad form to call a classmethod as a method from an instance?

怎甘沉沦 提交于 2019-11-30 17:11:17
Ex. If I have something like this: class C(object): @classmethod def f(cls, x): return x + x This will work: c = C() c.f(2) 4 But is that bad form? Should I only call C.f() or c.__class__.f() Obviously, this would only make sense in cases where f doesn't interact with self/cls expecting it to be class. ? If you are tempted to call a class method from an instance you probably don't need a class method. In the example you gave a static method would be more appropriate precisely because of your last remark (no self/cls interaction). class C(object): @staticmethod def f(x): return x + x this way

What's the difference between class methods and instance methods in Swift?

南笙酒味 提交于 2019-11-30 17:03:30
问题 protocol NoteProtocol { var body: NSString? { get set } var createdAt: NSDate? { get set } var entityId: NSString? { get set } var modifiedAt: NSDate? { get set } var title: NSString? { get set } // class methods class func insertNewNoteInManagedObjectContext(managedObjectContext: NSManagedObjectContext!) -> NoteProtocol class func noteFromNoteEntity(noteEntity: NSManagedObject) -> NoteProtocol // instance methods func update(#title: String, body: String) func deleteInManagedObjectContext

super and __new__ confusion

两盒软妹~` 提交于 2019-11-30 10:59:41
问题 As what I just learned, I can use super() this way: super(class, obj_of_class-or-_subclass_of_class) Code goes below: #Case 1 class A(object): def __init__(self): print "A init" class B(A): def __init__(self): print "B init" super(B, self).__init__() #ok, I can invoke A's __init__ successfully #Case 2 class A(object): @classmethod def foo(cls): print "A foo" class B(object): @classmethod def foo(cls): print "B foo" super(B, cls).foo() #ok, I can invoke A's foo successfully #Case 3 class A

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

荒凉一梦 提交于 2019-11-30 09:55:16
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) with the idea being that someone can do >>> silly_walk = SillyWalk() >>> appraise = walk() >>> is_good

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

做~自己de王妃 提交于 2019-11-30 09:44:42
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? If you want the descriptor property to trigger when you get an attribute from object X, then you must put the descriptor in type(X) . So if X is a class, the

Why does a classmethod's super need a second argument?

瘦欲@ 提交于 2019-11-30 03:23:17
问题 This works as expected: >>> class Foo(object): ... @classmethod ... def hello(cls): ... print 'hello, foo' ... >>> class Bar(Foo): ... @classmethod ... def hello(cls): ... print 'hello, bar' ... super(Bar, cls).hello() ... >>> b = Bar() >>> b.hello() hello, bar hello, foo I can also call the base class explicitly: >>> class Bar(Foo): ... @classmethod ... def hello(cls): ... print 'hello, bar' ... Foo.hello() ... >>> b = Bar() >>> b.hello() hello, bar hello, foo I was wondering why I can't

Python: How to call an instance method from a class method of the same class

时光毁灭记忆、已成空白 提交于 2019-11-30 02:51:01
问题 I have a class as follows: class MyClass(object): int = None def __init__(self, *args, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) def get_params(self): return {'int': random.randint(0, 10)} @classmethod def new(cls): params = cls.get_params() return cls(**params) and I would like to be able to do: >>> obj = MyClass.new() >>> obj.int # must be defined 9 I mean without creating a new instance of MyClass , but obviously it's not that simple, because calling MyClass.new()

Python - can I programmatically decorate class methods from a class instance?

为君一笑 提交于 2019-11-30 01:51:34
问题 I have an object hierarchy in which almost all of the methods are class methods. It looks like the following: class ParentObject(object): def __init__(self): pass @classmethod def smile_warmly(cls, the_method): def wrapper(kls, *args, **kwargs): print "-smile_warmly - "+kls.__name__ the_method(*args, **kwargs) return wrapper @classmethod def greetings(cls): print "greetings" class SonObject(ParentObject): @classmethod def hello_son(cls): print "hello son" @classmethod def goodbye(cls): print