class-method

Use inherited class method within __init__

南楼画角 提交于 2019-12-02 07:40:40
问题 I have a parent class that is inherited by several children. I would like to initialize one of the children using the parent's @classmethod initializers. How can I do this? I tried: class Point(object): def __init__(self,x,y): self.x = x self.y = y @classmethod def from_mag_angle(cls,mag,angle): x = mag*cos(angle) y = mag*sin(angle) return cls(x=x,y=y) class PointOnUnitCircle(Point): def __init__(self,angle): Point.from_mag_angle(mag=1,angle=angle) p1 = Point(1,2) p2 = Point.from_mag_angle(2

Python noob can't get class method to work

主宰稳场 提交于 2019-12-02 05:54:53
I have a method in my Customer class called save_from_row() . It looks like this: @classmethod def save_from_row(row): c = Customer() c.name = row.value('customer', 'name') c.customer_number = row.value('customer', 'number') c.social_security_number = row.value('customer', 'social_security_number') c.phone = row.value('customer', 'phone') c.save() return c When I try to run my script, I get this: Traceback (most recent call last): File "./import.py", line 16, in <module> Customer.save_from_row(row) TypeError: save_from_row() takes exactly 1 argument (2 given) I don't understand the mismatch in

Get all instances of a class in objective c?

半城伤御伤魂 提交于 2019-12-02 01:16:32
I have a UIView that has many instances and each one of them has a UIRecognizer. When on of them is tapped I want to remove all the recognizers of the others. What I want it to get all the instances of the class and remove their recognizes. I know ManagedObjects has [Entity allObjects]; How can I create my "all objects" class method ? I have two ideas: 1/ Create a class array with all the instances static NSArray* instances; , register them when initializing, unregister when deallocating. The array should have only weak references, otherwise they will never be deallocated. 2/ NSNotification.

Is Class declaration an eyewash in ruby? Is everything really object oriented?

不问归期 提交于 2019-12-02 00:58:42
class Person def name puts "Dave" end end puts Person.object_id There are only two ways of accessing methods : 1) Someclass.method in case of class methods. #where Someclass is a class. 2) and Object.method when the method being accessed is a regular method declared inside a class. and Object is an instance of a class. It follows the pattern Object.method so, does it mean Person class is really an object? or object_id is a class method? The latter seems unlikely because class methods cannot be inherited into an instance. but when we do something like this : a = Person.new a.methods.include?(

Why compilation fails when a template parameter name matches an inner class name?

社会主义新天地 提交于 2019-12-01 05:35:14
Following compiles perfectly fine: struct MyClass { template<typename SameName> void foo (SameName* p); }; struct SameName {}; template<class SameName> void MyClass::foo (SameName* p) {} However, if we enclose MyClass and SameName inside some class Outer then the template function defined outside, fails to compile. struct Outer { /* paste here `MyClass` & `SameName` from above */ }; template<class SameName> void Outer::MyClass::foo (SameName* p) {} // <--- error here // ^^^^^ The g++ (03-14) error is weird: error: prototype for ‘void Outer::MyClass::foo(Outer::SameName*)’ does not match any in

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

旧城冷巷雨未停 提交于 2019-12-01 03:20:27
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 <Counter> +(NSInteger) countFor: (Model *)model; @end I then have an array of the classes that conform to this protocol (like CurrentListCounter does) +(NSArray *) availableCounters { return [[[NSArray alloc] initWithObjects: [CurrentListCounter class], [AllListsCounter

Why compilation fails when a template parameter name matches an inner class name?

一笑奈何 提交于 2019-12-01 03:12:00
问题 Following compiles perfectly fine: struct MyClass { template<typename SameName> void foo (SameName* p); }; struct SameName {}; template<class SameName> void MyClass::foo (SameName* p) {} However, if we enclose MyClass and SameName inside some class Outer then the template function defined outside, fails to compile. struct Outer { /* paste here `MyClass` & `SameName` from above */ }; template<class SameName> void Outer::MyClass::foo (SameName* p) {} // <--- error here // ^^^^^ The g++ (03-14)

Why should I use a classmethod in python? [duplicate]

爷,独闯天下 提交于 2019-12-01 01:09:34
问题 This question already has answers here : Meaning of @classmethod and @staticmethod for beginner? [duplicate] (12 answers) What is the purpose of class methods? (16 answers) Closed 2 years ago . I am writing a function in some class in python, and people suggested to me to add to this function a @classmethod decorator. My code: import random class Randomize: RANDOM_CHOICE = 'abcdefg' def __init__(self, chars_num): self.chars_num = chars_num def _randomize(self, random_chars=3): return ''.join

Duck typing and class methods (or, how to use a method from both a class and an instance?)

家住魔仙堡 提交于 2019-12-01 01:05:17
I have some code which I would like to pass instances or classes interchangeably. All I will do in that code is to call a method that I expect both classes and instances to have (the method go() in the example below). Unfortunately, I can't create a classmethod with the same name of a regular method... See example below. I initially expected the second call to produce an a instead of a b . Any advice on how to achieve this? Type "help", "copyright", "credits" or "license" for more information. >>> class A(object): ... def go(self): ... print "a" ... @classmethod ... def go(cls): ... print "b"

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

浪子不回头ぞ 提交于 2019-12-01 00:12:37
问题 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 <Counter> +(NSInteger) countFor: (Model *)model; @end I then have an array of the classes that conform to this protocol (like CurrentListCounter does) +(NSArray *)