static-methods

Why use staticmethod instead of no decorator at all

徘徊边缘 提交于 2021-02-19 04:34:04
问题 There are several good explanations on SO about why/when you should use a class method vs a static method, but I've not been able to find an answer for when you would use a static method over no decoration at all. Consider this class Foo(object): @staticmethod def f_static(x): print("static version of f, x={0}".format(x)) def f_standalone(x): print("standalone verion of f, x={0}".format(x)) And some output: >>> F = Foo >>> f = F() >>> F.f_static(5) static version of f, x=5 >>> F.f_standalone

Are DAO objects better than static DAO classes in PHP?

拜拜、爱过 提交于 2021-02-08 08:58:22
问题 I understand the reasons for not using statics in Java. However, I'm currently developing OO code in PHP. I use DAOs with the goal of keeping my queries in one place so I can easily find them. I also instantiate some DAOs so I can incorporate pagination in some (relevant) queries. In many cases, it's not necessary and so I tend to just create static methods (even though technically I don't think I can call that a DAO) in the form: $info = schemeDAO::someFunction($variable); I may need only

Accessing static method through an object instance

独自空忆成欢 提交于 2021-01-27 13:42:01
问题 I have learnt that if we wish to call static method of another class then you have to write class name while calling static method. In below program, I created object of the Employee class inside Employee_Impl class and using that object, I was still able to access the count method. Why is it allowing me to use count method through an object if static methods are accessed using only class name? Does that mean static methods can be accessed using objects as well as class name, both? Employee

Unable to mock static methods with parameters using Mockito

可紊 提交于 2020-12-15 05:51:55
问题 I am trying to use some of the new features of Mockito, specifically mocking of static methods. I am able to get it to work when the method I am mocking has no parameters, but for some reason it will not work if the method has any parameters. As the example below is written, the test assertEquals( "bar", Foo.foo() ) works but the test assertEquals(2, map.size() ) fails as no behavior has been defined for the mocked class. fooMock.when(Foo::genMap).thenCallRealMethod() gives the follwing

python how to call static method from inside of a class body [duplicate]

我只是一个虾纸丫 提交于 2020-07-05 12:35:55
问题 This question already has answers here : Calling class staticmethod within the class body? (5 answers) Closed 2 years ago . Let's assume I have a class, with a static method, and I want a class property to be set to the value that this method returns: class A: @staticmethod def foo(): return 12 baz = foo() But doing this I get an error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in A TypeError: 'staticmethod' object is not callable I found a

Static lambdas in python

时间秒杀一切 提交于 2020-03-21 16:09:59
问题 I have class in python like this import numpy as np class BackPropogationNetwork: # Static lambdas sigmoid = lambda x : 1/(1+np.exp(-x)) sigmoid_prime = lambda sigmoid: sigmoid(1-sigmoid) and this is the contructor def __init__(self): self.some_attr = self.sigmoid(2) I get this error TypeError: <lambda>() takes exactly 1 argument (2 given) If I call like this self.some_attr = ClassName.sigmoid() I get this error TypeError: unbound method <lambda>() must be called with BackPropogationNetwork

Static lambdas in python

南笙酒味 提交于 2020-03-21 16:07:16
问题 I have class in python like this import numpy as np class BackPropogationNetwork: # Static lambdas sigmoid = lambda x : 1/(1+np.exp(-x)) sigmoid_prime = lambda sigmoid: sigmoid(1-sigmoid) and this is the contructor def __init__(self): self.some_attr = self.sigmoid(2) I get this error TypeError: <lambda>() takes exactly 1 argument (2 given) If I call like this self.some_attr = ClassName.sigmoid() I get this error TypeError: unbound method <lambda>() must be called with BackPropogationNetwork