class-method

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

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) fo...

How bad is it to override a method from a third-party module?

How bad is it to redefine a class method from another, third-party module, in Python? In fact, users can create NumPy matrices that contain numbers with uncertainty; ideally, I would like their code to run unmodified (compared to when the code manipulates float matrices); in particular, it would be great if the inverse of matrix m could...

How can I make a UIButton perform my class method when tapped?

How can I make a UIButton perform my class method, +doSomething:(id)sender, on class MyClass, after any event like UIControlEventTouchDown? Can I use -addTarget:action:forControlEvents: like I do for instance methods? Thanks in advance. ...

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

>>> 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 i...

Ruby metaclass madness

I'm stuck. I'm trying to dynamically define a class method and I can't wrap my head around the ruby metaclass model. Consider the following class: class Example def self.meta; (class << self; self; end); end def self.class_instance; self; end end Example.class_instance.class # => Class Example.meta.class # => Class Ex...

Drawbacks with using Class Methods in Objective C.

Hi I was wondering if there are any memory/performance drawbacks, or just drawbacks in general, with using Class Methods like: + (void)myClassMethod:(NSString *)param { // much to be done... } or + (NSArray*)myClassMethod:(NSString *)param { // much to be done... return [NSArray autorelease]; } It is convenient placing a lot o...

Invoking a superclass's class methods in Python

I am working on a Flask extension that adds CouchDB support to Flask. To make it easier, I have subclassed couchdb.mapping.Document so the store and load methods can use the current thread-local database. Right now, my code looks like this: class Document(mapping.Document): # rest of the methods omitted for brevity @classmethod de...

Class Methods Inheritence

I was told that static methods in java didn't have Inheritance but when I try the following test package test1; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { TB.ttt(); TB.ttt2(); } } package test1; public class TA { static publi...

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

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: ...

Passing an identical message to multiple ObjC classes

I have multiple classes, all of which I want to send an identical message to. To be clearer: I want to send doX:withClass: with the same parameters to a number of classes. Perhaps code would make it clearer: + (void)doX:(NSString *)blah { [Utility doX:blah withClass:[Foo class]]; [Utility doX:blah withClass:[Bar class]]; ...

Objective-c: How can I get Class instance in class method.

I have 2 classes, Parent and Child, and Parent has a class method named func. Now I want to get Class instance in func method to distinguish which class is caller. @interface Parent : NSObject + (void)func; @end @implementation Parent + (void)func { Class *class = howToGetClass(); NSLog(@"%@ call func", class); } @end @inter...

__getattr__ for static/class variables in python

I have a class like: class MyClass: Foo = 1 Bar = 2 Whenever MyClass.Foo or MyClass.Bar is invoked, I need a custom method to be invoked before the value is returned. Is it possible in Python ? I know it is possible if I create an instance of the class and I can define my own __getattr__ method. But my scnenario involves usi...

If I define a class-method in Ruby Object class, how do I get the name of a child class calling this method?

Example def Object.const_missing(name) puts self.class end class A; end A::B # => Class How can I get A in Object#const_missing? ...

Ruby modules and extend self

In what sort of situation is the code: module M extend self def greet puts "hello" end end more beneficial to use over say something like: module M def self.greet puts "hello" end end In the top, one is an instance method being extended, and the latter is just a class method, but when calling either method,...

How to dynamically define a class method which will refer to a local variable outside?

class C end var = "I am a local var outside" C.class_eval do def self.a_class_method puts var end # I know, this is not correct, because the 'def' created a new scope; # I am asking a solution to make it; # I also know that use 'define_method' can create a instance method without creating a new scope. # but my...

Accessing the name of an instance in Python (so I can print it)

So as part of problem 17.6 in "Think Like a Computer Scientist", I've written a class called Kangaroo: class Kangaroo(object): def __init__(self, pouch_contents = []): self.pouch_contents = pouch_contents def __str__(self): ''' >>> kanga = Kangaroo() >>> kanga.put_in_pouch('olfactory') >...

Create decorator that can see current class method

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): ...

Why isn't this classprop implementation working?

Based on a question I previously asked, I tried to come up with a class property that would allow setting as well as getting. So I wrote this and put it in a module util: class classprop(object): def __init__(self, fget, fset=None): if isinstance(fget, classmethod): self.fget = fget else: sel...