instance-variables

Java instance variables initialization with method

独自空忆成欢 提交于 2019-12-01 02:33:09
I am a little bit confused about the following piece of code: public class Test{ int x = giveH(); int h = 29; public int giveH(){ return h; } public static void main(String args[]) { Test t = new Test(); System.out.print(t.x + " "); System.out.print(t.h); } } The output here is 0 29 , but I thought that this has to be a compiler error, because the variable h should have not been initialized when it comes to the method giveH() . So, does the compilation go through the lines from top to bottom? Why is this working? Why is the value of x 0 and not 29? The default value of int is 0 (see here ).

Calling class variable with self

泪湿孤枕 提交于 2019-12-01 00:47:41
How would you I came up with this interesting (at least to me) example. import numpy as np class Something(object): a = np.random.randint(low=0, high=10) def do(self): self.a += 1 print(self.a) if __name__ == '__main__': something = Something() print(something.__str__()) something.do() something2 = Something() print(something2.__str__()) something2.do() something3 = Something() print(something3.__str__()) something3.do() The above prints the following in the console: $ python test.py <__main__.Something object at 0x7f03a80e0518> 1 <__main__.Something object at 0x7f03a80cfcc0> 1 <__main__

What is the python attribute get and set order?

白昼怎懂夜的黑 提交于 2019-11-30 20:45:48
Python provides us many possibilities on instance/class attribute, for example: class A(object): def __init__(self): self.foo = "hello" a = A() There are many ways to access/change the value of self.foo : direct access a.foo inner dict a.__dict__['foo'] get and set a.__get__ and a.__set__ ,of course there two are pre-defined methods. getattribute a.__getattribute__ __getattr__ and __setattr__ maybe more. While reading source code, I always get lost of what's their ultimate access order? When I use a.foo , how do I know which method/attribute will get called actually? bar = a.foo ... invokes a.

Must all Python instance variables be declared in def __init__?

天大地大妈咪最大 提交于 2019-11-30 14:08:11
Or can they be declared otherwise? The code below does not work: class BinaryNode(): self.parent = None self.left_child = None Do they need to be declared in __init__ ? They do not have to be declared in __init__ , but in order to set an instance variable using self , there needs to be a reference to self , and the place you are defining the variables does not. However, class BinaryNode(): parent = None left_child = None def run(self): self.parent = "Foo" print self.parent print self.left_child The output will be Foo None To answer your question in the comment, yes. You can, in my example say:

Type Hints Convention for Instance Variables Python

…衆ロ難τιáo~ 提交于 2019-11-30 13:55:02
Unsure of the Python convention for type hinting instance variables - I've been doing them within the __init__ constructor arguments like seen here: class LoggedVar(Generic[T]): def __init__(self, value: T, name: str, logger: Logger) -> None: self.name = name self.logger = logger self.value = value` link to above code snippet: https://docs.python.org/3/library/typing.html#user-defined-generic-types But I also see the PEP conventions of annotating instance variables as such(snippet below) and then also doing type hinting within the __init__ arguments: class BasicStarship: captain: str = 'Picard

What is the correct (or best) way to subclass the Python set class, adding a new instance variable?

吃可爱长大的小学妹 提交于 2019-11-30 12:37:50
I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of this variable is copied when one of my objects is copied? Using the old sets module, the following code worked perfectly: import sets class Fooset(sets.Set): def __init__(self, s = []): sets.Set.__init__(self, s) if isinstance(s, Fooset): self.foo = s.foo else: self.foo = 'default' f = Fooset([1,2,4]) f.foo = 'bar' assert( (f | f).foo == 'bar') but this does not work using the built-in set module.

Object of the class as instance variable inside the class [duplicate]

痴心易碎 提交于 2019-11-30 12:12:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How can a class have a member of its own type, isnt this infinite recursion? The Code: public class Test2{ private Test2 subject = new Test2(); //Create Test2 object in Test2 private int num; } The Questions: Why does Java permit the above code to be executed, but C++ doesn't? Does the code above create infinite number of objects? Since Test2 itself contains a Test2 object which again contains a Test2 object

Inherit class-level instance variables in Ruby?

帅比萌擦擦* 提交于 2019-11-30 08:00:55
问题 I want a child class to inherit a class-level instance variable from its parent, but I can't seem to figure it out. Basically I'm looking for functionality like this: class Alpha class_instance_inheritable_accessor :foo # @foo = [1, 2, 3] end class Beta < Alpha @foo << 4 def self.bar @foo end end class Delta < Alpha @foo << 5 def self.bar @foo end end class Gamma < Beta @foo << 'a' def self.bar @foo end end And then I want this to output like this: > Alpha.bar # [1, 2, 3] > Beta.bar # [1, 2,

Why instance variable in Servlet is not thread-safe [duplicate]

為{幸葍}努か 提交于 2019-11-30 07:29:36
This question already has an answer here: How do servlets work? Instantiation, sessions, shared variables and multithreading 8 answers When I read Head First Servlet and JSP , they say that instance variable is non-thread safe. I don't understand this statement so much. For example: I have a servlet which name is ActionServlet.java . Each time, each user's request is sent to server, container will create a new thread and create new ActionServlet instance. ActionServlet may be has a structure: public class ActionServlet extends HttpServlet { // example of instance variable Instance variable;

Same instance variable for all actions of a controller

感情迁移 提交于 2019-11-30 06:33:53
问题 I have a rails controller with two actions defined: index and show . I have an instance variable defined in index action. The code is something like below: def index @some_instance_variable = foo end def show # some code end How can I access the @some_instance_variable in show.html.erb template? 回答1: Unless you're rendering show.html.erb from the index action, you'll need to set @some_instance_variable in the show action as well. When a controller action is invoked, it calls the matching