static-methods

super() and @staticmethod interaction

余生长醉 提交于 2019-11-28 20:07:57
Is super() not meant to be used with staticmethods? When I try something like class First(object): @staticmethod def getlist(): return ['first'] class Second(First): @staticmethod def getlist(): l = super(Second).getlist() l.append('second') return l a = Second.getlist() print a I get the following error Traceback (most recent call last): File "asdf.py", line 13, in <module> a = Second.getlist() File "asdf.py", line 9, in getlist l = super(Second).getlist() AttributeError: 'super' object has no attribute 'getlist' If I change the staticmethods to classmethods and pass the class instance to

`staticmethod` and `abc.abstractmethod`: Will it blend?

和自甴很熟 提交于 2019-11-28 17:17:09
问题 In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this? I tried applying both decorators, but it doesn't work. If I do this: import abc class C(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @staticmethod def my_function(): pass I get an exception*, and if I do this: class C(object): __metaclass__ = abc.ABCMeta @staticmethod @abc.abstractmethod def my_function(): pass The abstract method is not enforced. How can I make an

Are static methods more efficient?

試著忘記壹切 提交于 2019-11-28 16:49:36
In terms of memory and time, is it better to make a method static? Usually yes, there is no need to pass the "this" reference. That reference is passed in the ECX register so there is no additional stack space required. The register is already set if you make the call from an instance method of the same class, there will be no savings at all. But it can help relieving the pressure on a x86 CPU core when the method is in another class, x86 doesn't have a lot of registers. Seeing a measurable perf improvement would be exceedingly rare. I do religiously mark methods of a class that don't use

Why is a static method considered a method?

旧巷老猫 提交于 2019-11-28 16:08:01
I'm writing an explanation for some code for a course, and have been accidentally using the words method and function interchangeably. I decided to go back over and fix the wording, but ran into a hole in my understanding. From what I understand, a subroutine is a function if it doesn't act on an instance of a class (its effect is restricted to its explicit input/output), and is a method if it operates on an instance of a class (it may carry out side effects on the instance that make it impure). There's a good discussion here on the topic. Note that by the accepted answer's definitions, a

Static methods - How to call a method from another method?

百般思念 提交于 2019-11-28 15:54:57
问题 When I have regular methods for calling another method in a class, I have to do this class test: def __init__(self): pass def dosomething(self): print "do something" self.dosomethingelse() def dosomethingelse(self): print "do something else" but when I have static methods I can't write self.dosomethingelse() because there is no instance. How do I have to do in Python for calling an static method from another static method of the same class? Edit: what a mess. Ok, I edited back the question to

Java: Calling a static method in the main() method

柔情痞子 提交于 2019-11-28 14:12:45
I am supposed to do the following: Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. You could either use an array or an ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. You could possibly think a range of values like 1 – 4. If random value is 1, create a HourlyEmployee object with some randomly generated data, if 2, SalariedEmployee object with some random data and so on. I

Why Static method sometimes returns same result for separate call?

大城市里の小女人 提交于 2019-11-28 13:04:49
In my c# code I have a static method. Here is the code sample: public class RandomKey { public static string GetKey() { Random rng = new Random(); char[] chars = new char[8]; for (int i = 0; i < chars.Length; i++) { int v = rng.Next(10 + 26 + 26); char c; if (v < 10) { c = (char)('0' + v); } else if (v < 36) { c = (char)('a' - 10 + v); } else { c = (char)('A' - 36 + v); } chars[i] = c; } string key = new string(chars); key += DateTime.Now.Ticks.ToString(); return key; } } I am calling this function from another Class's method. Class SomeClass { Void SomeMethod() { for(int i=0; i<100; i++) {

How to get (sub)class name from a static method in Python?

跟風遠走 提交于 2019-11-28 11:53:04
If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): # code pass Is it possible for a function call Foo.bar() to determine the class name Foo? Replace the staticmethod with a classmethod. This will be passed the class when it is called, so you can get the class name from that. class Bar(object): @classmethod def bar(cls): # code print cls.__name__ class Foo(Bar): # code pass >>> Bar.bar() Bar >>> Foo.bar() Foo If you need to find the class information, the appropriate way is to use @classmethod . class Bar(object): @classmethod def bar(cls): # code print(cls._

How can I solve \"Non-static method xxx:xxx() should not be called statically in PHP 5.4?

混江龙づ霸主 提交于 2019-11-28 10:41:10
Currently using a large platform in PHP. The server it's hosted on has recently been upgraded to PHP 5.4. Since, I've received many error messages like: [Sat May 26 19:04:41 2012] [error] PHP Strict Standards: Non-static method Config::getData() should not be called statically, assuming $this from incompatible context in /xxx/Config.inc.php on line 35 The example method is defined as (note the lack of 'static' keyword): function &getData() { $configData =& Registry::get('configData', true, null); if ($configData === null) { // Load configuration data only once per request, implicitly // sets

How do I access an instance variable inside a BindingAdapter when using Android Data Binding?

不问归期 提交于 2019-11-28 07:59:59
问题 So I'm using this popular data binding code snippet to load in image into imageview of list items by passing in URL: <ImageView android:layout_width="match_parent" android:layout_height="150dp"" app:imageUrl="@{movie.imageUrl}" /> The Binding Adapter: class Movie{ boolean isLoaded; @BindingAdapter({"bind:imageUrl"}) public static void loadImage(final ImageView view, String imageUrl) { Picasso.with(view.getContext()) .load(imageUrl) .into(view, new Callback.EmptyCallback() { @Override public