class-method

What does 'self' refer to in a @classmethod?

I thought I was starting to get a grip on "the Python way" of programming. Methods of a class accept self as the first parameter to refer to the instance of the class whose context the method is being called in. The @classmethod decorator refers to a method whose functionality is associated with the class, but which doesn't reference a s...

Object retain behavior of Objective-C class methods

What's the best practice for retaining and releasing objects passed to class methods? For instance, if you have a "class variable" declared like so: static NSString *_myString = nil ...is the right thing to do this: + (void)myClassMethod:(NSString *)param { _myString = param; } ... which has the drawback that the caller needs ...

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

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

How do I use define_method to create class methods?

This is useful if you are trying to create class methods metaprogramatically: def self.create_methods(method_name) # To create instance methods: define_method method_name do ... end # To create class methods that refer to the args on create_methods: ??? end My answer to follow... ...

classmethod for Tkinter-Monitor-Window

Hello together, I would like to realise a monitor window that reports the user about ongoing computations. To do so I wrote a little class. But as I would like to use it accross different modules in an easy fashion I thought to implement it with classmethods. This allows to use it in the following way without instances: from MonitorMod...

In Ruby are there any related applications of the syntax: class << self ... end

class << self attr_accessor :n, :totalX, :totalY end The syntax above is used for defining class instance variables. But when I think about what syntax implies, it doesn't make any sense to me, so I'm wondering if this type of syntax is used for any other types of definitions. My point of confusion here is this: class << self The ap...

How to understand the difference between class_eval() and instance_eval() ?

Foo = Class.new Foo.class_eval do def class_bar "class_bar" end end Foo.instance_eval do def instance_bar "instance_bar" end end Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class Foo.new.class_bar #=> "class_bar" Foo.instance_bar #=> "instance_bar" Foo.new.instance_bar #=> undefined method ‘inst...

Why does instance_eval() define a class method when called on a class?

Foo = Class.new Foo.instance_eval do def instance_bar "instance_bar" end end puts Foo.instance_bar #=> "instance_bar" puts Foo.new.instance_bar #=> undefined method ‘instance_bar’ My understanding is that calling instance_eval on an object is supposed to allow you to define an instance variable or method for that object...

How do I call +class methods in Objective C without referencing the class?

I have a series of "policy" objects which I thought would be convenient to implement as class methods on a set of policy classes. I have specified a protocol for this, and created classes to conform to (just one shown below) @protocol Counter +(NSInteger) countFor: (Model *)model; @end @interface CurrentListCounter : NSObject <Count...

Is there a difference between the ways of defining a Ruby class method?

Given the following two ways of defining a class method in Ruby: class Foo class << self def bar # ... end end def self.baz # ... end end Is there a difference between the two? Is one more preferable than the other? ...

Python class method - Is there a way to make the calls shorter?

I am playing around with Python, and I've created a class in a different package from the one calling it. In this class, I've added a class method which is being called from my main function. Again, they are in separate packages. The line to call the class method is much longer than I thought it would be from the examples I've seen in...

Calling C++ class methods via a function pointer

How do I obtain a function pointer for a class member function, and later call that member function with a specific object? I’d like to write: class Dog : Animal { Dog (); void bark (); } … Dog* pDog = new Dog (); BarkFunction pBark = &Dog::bark; (*pBark) (pDog); … Also, if possible, I’d like to invoke the constructor via a ...

Cocoa-Touch. What Exactly is the Difference Between These NSMutableData Methods?

One thing I'm a bit unclear on is the difference between these NSMutableArray Methods: // Class Method Style NSMutableData *myMutableDataInstance = [NSMutableData dataWithLength:WholeLottaData]; and // Instance Method Style NSMutableData *myMutableDataInstance = nil; myMutableDataInstance = [[[NSMutableData alloc] initWithLength:...

How does a classmethod object work?

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

Using super with a class method

I'm trying to learn myself the super() function in Python. I though I got a grasp of it untill 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,...

Why does my Python class claim that I have 2 arguments instead of 1?

#! /usr/bin/env python import os import stat import sys class chkup: def set(file): filepermission = os.stat(file) user_read() user_write() user_exec() def user_read(): """Return True if 'file' is readable by user """ ...

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

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

Python-style classmethod for C#?

Is there a way to do what classmethod does in Python in C#? That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it's used from. An example of what I want, approximately, is class Base: @classmethod def get(cls, id): print "Would instantiate a new %r with ID...

Delegating instance methods to the class method

In Ruby, suppose I have a class Foo to allow me to catalogue my large collection of Foos. It's a fundamental law of nature that all Foos are green and spherical, so I have defined class methods as follows: class Foo def self.colour "green" end def self.is_spherical? true end end This lets me do Foo.colour # "green" ...

Calling An Inherited Class Method From Java

In Python, class methods can be inherited. e.g. >>> class A: ... @classmethod ... def main(cls): ... return cls() ... >>> class B(A): pass ... >>> b=B.main() >>> b <__main__.B instance at 0x00A6FA58> How would you do the equivalent in Java? I currently have: public class A{ public void show(){ System.out.println("A");...