class-method

Python - why can I call a class method with an instance?

让人想犯罪 __ 提交于 2020-05-08 06:53:07
问题 New to Python and having done some reading, I'm making some methods in my custom class class methods rather than instance methods. So I tested my code but I hadn't changed some of the method calls to call the method in the class rather than the instance, but they still worked: class myClass: @classmethod: def foo(cls): print 'Class method foo called with %s.'%(cls) def bar(self): print 'Instance method bar called with %s.'%(self) myClass.foo() thing = myClass() thing.foo() thing.bar() This

Is this sound software engineering practice for class construction?

混江龙づ霸主 提交于 2020-04-30 08:00:24
问题 Is this a plausible and sound way to write a class where there is a syntactic sugar @staticmethod that is used for the outside to interact with? Thanks. ###scrip1.py### import SampleClass.method1 as method1 output = method1(input_var) ###script2.py### class SampleClass(object): def __init__(self): self.var1 = 'var1' self.var2 = 'var2' @staticmethod def method1(input_var): # Syntactic Sugar method that outside uses sample_class = SampleClass() result = sample_class._method2(input_var) return

Child Class constructor using super() - getting unbound method __init__()

江枫思渺然 提交于 2020-02-04 13:27:20
问题 I'm trying to create classmethod constructors for a child class, but I cannot initialize the instance properly. I have read many blogs and answers on this site and even tried exactly what some other people have posted, still to no avail. Hopefully I'm missing something really simple. Basic example of what I'm trying: class A(object): def __init__(self, foo): self.foo = foo class B(A): @classmethod def make_new(cls): super(B, cls).__init__('bar') foobar = B.make_new() I keep getting unbound

Child Class constructor using super() - getting unbound method __init__()

*爱你&永不变心* 提交于 2020-02-04 13:26:45
问题 I'm trying to create classmethod constructors for a child class, but I cannot initialize the instance properly. I have read many blogs and answers on this site and even tried exactly what some other people have posted, still to no avail. Hopefully I'm missing something really simple. Basic example of what I'm trying: class A(object): def __init__(self, foo): self.foo = foo class B(A): @classmethod def make_new(cls): super(B, cls).__init__('bar') foobar = B.make_new() I keep getting unbound

Access Class method and variable using self

♀尐吖头ヾ 提交于 2020-01-24 01:45:07
问题 In below example Test class has two instance method and one classmethod In set_cls_var_1 method I set class variable using self. In set_cls_var_2 method I call class method using self. class Test(): #class variable cls_var = 10 def __init__(self): obj_var=20 def set_cls_var_1(self,val): #second method to access class variable print "first " self.cls_var = val def set_cls_var_2(self): print "second" self.task(200) @classmethod def task(cls,val): cls.cls_var = val t=Test() #set class variable

Python set docstring and get method name of dynamically generated classmethod

一笑奈何 提交于 2020-01-14 03:28:26
问题 I'm trying to get/set the name and docstring of dynamically created class methods as follows, but am having trouble figuring out exactly how to do it: import sys import inspect class test(object): pass @classmethod def genericFunc(cls, **kwargs): print "function:", (inspect.stack()[0][3]) print "kwargs:", kwargs function_list = ['myF1', 'myF2'] for func in function_list: setattr(test, func, genericFunc) #set docstring for func here? if __name__ == '__main__': x = test() print "docstring:", x

Check if a function uses @classmethod

十年热恋 提交于 2020-01-12 03:26:34
问题 TL;DR How do I find out whether a function was defined using @classmethod or something with the same effect? My problem For implementing a class decorator I would like to check if a method takes the class as its first argument, for example as achieved via @classmethod def function(cls, ...): I found a solution to check for @staticmethod via the types module ( isinstance(foo, types.UnboundMethodType) is False if the foo is static, see here), but did not find anything on how to do so for

@classmethod for constructor overloading

主宰稳场 提交于 2020-01-05 06:37:07
问题 I usually use isinstance for constructor overloading, but people also suggests @classmethod for the same. But to my knowledge @classmethod shares the variable. Below is a simple class class abc: def __init__(self, a=0): self.a = a @classmethod def from_const(cls, b=30): cls.b = b return cls(10) def printme(self): print(self.a,self.b) Now, lets make three objects a1 = abc(a=100) a2 = abc.from_const(b=31) a3 = abc.from_const(b=41) a4 = abc().from_const(b=51) a5 = abc().from_const(b=61) a1

Dynamically adding class methods to a class

非 Y 不嫁゛ 提交于 2020-01-02 04:45:05
问题 I have the following snippet: FEED_TYPES = [ ('fan_mail', 'Fan Mail'), ('review', 'Review'), ('tip', 'Tip'), ('fan_user', 'Fan User'), ('fan_song', 'Fan Song'), ('fan_album', 'Fan Album'), ('played_song', 'Played Song'), ('played_album', 'Played Album'), ('played_radio', 'Played Radio'), ('new_event', 'New Event'), ] class Feed: @classmethod def do_create(cls, **kwargs): print kwargs @classmethod def create(cls, type, **kwargs): kwargs['feed_type'] = type cls.do_create(**kwargs) for type

PHP get static methods

亡梦爱人 提交于 2020-01-02 03:37:09
问题 i want to call a class method by a var (like this): $var = "read"; $params = array(...); //some parameter if(/* MyClass has the static method $var */) { echo MyClass::$var($params); } elseif (/* MyClass hat a non-static method $var */) { $cl = new MyClass($params); echo $cl->$var(); } else throw new Exception(); i read in the php-manual how to get the function-members of a class (get_class_methods). but i get always every member without information if its static or not. how can i determine a