class-method

Create decorator that can see current class method

早过忘川 提交于 2019-12-05 11:53:58
Can you create a decorator inside a class that will see the classes methods and variables? The decorator here doesnt see: self.longcondition() class Foo: def __init__(self, name): self.name = name # decorator that will see the self.longcondition ??? class canRun(object): def __init__(self, f): self.f = f def __call__(self, *args): if self.longcondition(): # <-------- ??? self.f(*args) # this is supposed to be a very long condition :) def longcondition(self): return isinstance(self.name, str) @canRun # <------ def run(self, times): for i in xrange(times): print "%s. run... %s" % (i, self.name)

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

江枫思渺然 提交于 2019-12-05 03:49:24
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_method returns a MyInt . Here is a MCVE: from dataclasses import dataclass from typing import Union,

How can you call class methods on mailers when they're not defined as such?

怎甘沉沦 提交于 2019-12-05 02:08:25
When sending mail in Rails, usually one would do something like this: UserMailer.password_reset(user).deliver But if we look inside UserMailer we can see this: def password_reset(user) # not self.password_reset # ... end Notice that the method name is not prefixed with self . Looking at it, it seems like you need to instantiate the object first as below. How does Rails do this? UserMailer.new.password_reset(user).deliver That's a great question. In the source ( https://github.com/rails/rails/blob/master/actionmailer/lib/action_mailer/base.rb ), Rails uses method_missing to create a new

Using same function as instance and classmethod in python

夙愿已清 提交于 2019-12-04 22:35:26
问题 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

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

别等时光非礼了梦想. 提交于 2019-12-04 20:11:34
问题 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 回答1: [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

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

梦想的初衷 提交于 2019-12-04 12:57:44
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(object): @classmethod @mydec def bar(cls, baz='test', qux=None): print (baz, qux) Foo.bar() Unfortunately

Ruby: Calling class method from instance

心已入冬 提交于 2019-12-04 07:38:49
问题 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. 回答1: Rather than referring to the

How to add a classmethod in Python dynamically

心不动则不痛 提交于 2019-12-04 05:19:49
I'm using Python 3. I know about the @classmethod decorator. Also, I know that classmethods can be called from instances. class HappyClass(object): @classmethod def say_hello(): print('hello') HappyClass.say_hello() # hello HappyClass().say_hello() # hello However, I don't seem to be able to create class methods dynamically AND let them be called from instances. Let's say I want something like class SadClass(object): def __init__(self, *args, **kwargs): # create a class method say_dynamic SadClass.say_dynamic() # prints "dynamic!" SadClass().say_dynamic() # prints "dynamic!" I've played with

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

馋奶兔 提交于 2019-12-04 03:45:23
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; } One is bad, one is good, and the other is excellent. Why is that? bbum There is a fourth pattern.... (1)

(In Ruby) allowing mixed-in class methods access to class constants

馋奶兔 提交于 2019-12-04 02:12:55
I have a class with a constant defined for it. I then have a class method defined that accesses that class constant. This works fine. An example: #! /usr/bin/env ruby class NonInstantiableClass Const = "hello, world!" class << self def shout_my_constant puts Const.upcase end end end NonInstantiableClass.shout_my_constant My problem arises in attempting to move this class method out to an external module, like so: #! /usr/bin/env ruby module CommonMethods def shout_my_constant puts Const.upcase end end class NonInstantiableClass Const = "hello, world!" class << self include CommonMethods end