static-variables

Static variables in instance methods

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 11:38:46
Let's say I have this program: class Foo { public: unsigned int bar () { static unsigned int counter = 0; return counter++; } }; int main () { Foo a; Foo b; } (Of course this example makes no sense since I'd obviously declare "counter" as a private attribute, but it's just to illustrate the problem). I'd like to know how C++ behaves in this kind of situation: will the variable "counter" in the bar() method be the same for every instance? Yes, counter will be shared across all instances of objects of type Foo in your executable. As long as you're in a singlethreaded environment, it'll work as

Efficient memoization in Python

白昼怎懂夜的黑 提交于 2019-12-03 07:04:14
问题 I have some task to solve and the most important part at the moment is to make the script as time-efficient as possible. One of the elements I am trying to optimize is memoization within one of the functions. So my question is: Which of the following 3-4 methods is the most efficient / fastest method of implementing memoization in Python? I have provided code only as an example - if one of the methods is more efficient, but not in the case I mentioned, please share what you know. Solution 1 -

Efficient memoization in Python

守給你的承諾、 提交于 2019-12-02 20:42:41
I have some task to solve and the most important part at the moment is to make the script as time-efficient as possible. One of the elements I am trying to optimize is memoization within one of the functions. So my question is: Which of the following 3-4 methods is the most efficient / fastest method of implementing memoization in Python? I have provided code only as an example - if one of the methods is more efficient, but not in the case I mentioned, please share what you know. Solution 1 - using mutable variable from outer scope This solution is often shown as the example memoization, but I

Inherit a Static Variable in Java

旧街凉风 提交于 2019-12-01 20:51:51
I want to have the following setup: abstract class Parent { public static String ACONSTANT; // I'd use abstract here if it was allowed // Other stuff follows } class Child extends Parent { public static String ACONSTANT = "some value"; // etc } Is this possible in java? How? I'd rather not use instance variables/methods if I can avoid it. Thanks! EDIT: The constant is the name of a database table. Each child object is a mini ORM. you can't do it exactly as you want. Perhaps an acceptable compromise would be: abstract class Parent { public abstract String getACONSTANT(); } class Child extends

What exactly does “static” mean when declaring “global” variables in Java?

与世无争的帅哥 提交于 2019-12-01 16:47:06
I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on. public class Member { // Global Variables int iNumVertices; int iNumEdges; public static void main(String[] args) { // do stuff iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices // do more stuff } // main end } So eclipse tells me to do static int iNumVertices; and I'm not sure why. So what exactly is "static", how is it used, what is the purpose of using

Access static variable from static method

♀尐吖头ヾ 提交于 2019-12-01 15:02:21
I want to access a static variable from a static method: #!/usr/bin/env python class Messenger: name = "world" @staticmethod def get_msg(grrrr): return "hello " + grrrr.name print Messenger.get_msg(Messenger) How to do it without passing grrrr to a method? Is this the true OOP?.. Anything like name or self.name seems not working: NameError: global name 'name' is not defined and NameError: global name 'self' is not defined Dmitry Isaev Use @classmethod instead of @staticmethod . Found it just after writing the question. In many languages (C++, Java etc.) "static" and "class" methods are

Static variables in C#

好久不见. 提交于 2019-12-01 13:58:54
问题 In C#, is there a way to put a static variable in a method like VB.Net? Static myCollection As Collection 回答1: Why doesn't C# support static method variables? Q: In C++, it's possible to write a static method variable, and have a variable that can only be accessed from inside the method. C# doesn't provide this feature. Why? A: There are two reasons C# doesn't have this feature. First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would

Access static class variable of parent class in Python

六月ゝ 毕业季﹏ 提交于 2019-12-01 08:38:45
I have someting like this class A: __a = 0 def __init__(self): A.__a = A.__a + 1 def a(self): return A.__a class B(A): def __init__(self): # how can I access / modify A.__a here? A.__a = A.__a + 1 # does not work def a(self): return A.__a Can I access the __a class variable in B ? It's possible writing a instead of __a , is this the only way? (I guess the answer might be rather short: yes :) So, __a isn't a static variable, it's a class variable. And because of the double leading underscore, it's a name mangled variable. That is, to make it pseudo-private, it's been automagically renamed to _

Access static class variable of parent class in Python

拥有回忆 提交于 2019-12-01 07:47:16
问题 I have someting like this class A: __a = 0 def __init__(self): A.__a = A.__a + 1 def a(self): return A.__a class B(A): def __init__(self): # how can I access / modify A.__a here? A.__a = A.__a + 1 # does not work def a(self): return A.__a Can I access the __a class variable in B ? It's possible writing a instead of __a , is this the only way? (I guess the answer might be rather short: yes :) 回答1: So, __a isn't a static variable, it's a class variable. And because of the double leading

java static variable serialization

拥有回忆 提交于 2019-12-01 07:45:57
问题 How are the values of static variables persisted during serialization(If at all persisted). I have read similar questions on stack where it says that static variables are inherently transient i.e their state or current values is not serialized. I was just doing a very simple example where i serialized a class and saved it to a file and then again reconstructed the class from the file.Surprisingly I find that the value of the static variable at and when the serialization happened is saved. How