instance-variables

Python: How to share data between instances of different classes?

孤人 提交于 2019-12-19 07:55:13
问题 Class BigClassA: def __init__(self): self.a = 3 def foo(self): self.b = self.foo1() self.c = self.foo2() self.d = self.foo3() def foo1(self): # do some work using other methods not listed here def foo2(self): # do some work using other methods not listed here def foo3(self): # do some work using other methods not listed here Class BigClassB: def __init__(self): self.b = # need value of b from BigClassA self.c = # need value of c from BigClassA self.d = # need value of d from BigClassA def foo

Ruby: Automatically set instance variable as method argument?

时光总嘲笑我的痴心妄想 提交于 2019-12-19 05:37:16
问题 Are there any plans to implement ruby behavior similar to the CoffeeScript feature of specifying an instance variable name in a method argument list? Like class User def initialize(@name, age) # @name is set implicitly, but @age isn't. # the local variable "age" will be set, just like it currently works. end end I'm aware of this question: in Ruby can I automatically populate instance variables somehow in the initialize method? , but all the solutions (including my own) don't seem to fit the

Calling class variable with self

心已入冬 提交于 2019-12-19 04:29:21
问题 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_

Type Hints Convention for Instance Variables Python

泄露秘密 提交于 2019-12-18 12:34:30
问题 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

How do I set an attr_accessor for a dynamic instance variable?

可紊 提交于 2019-12-18 10:24:39
问题 I dynamically created an instance variable within my class: class Mine attr_accessor :some_var def intialize @some_var = true end def my_number num self.instance_variable_set "@my_#{num}", num end end How do I make @my_#{num} now as an attr value? e.g. I want to be able to do this: dude = Mine.new dude.my_number 1 dude.my_1 => 1 回答1: this answer doesn't pollutes the class space, example.. if i do mine.my_number 4 then the other instances of Mine will not get the my_4 method.. this happens

Which run first? default values for instance variables or Super Constructors?

我的梦境 提交于 2019-12-18 06:57:26
问题 According to the SCJP6 (Page 507) i found that instance variables are assigned default values before the superclass constructors complete, i tried an example in Debugg mode but i saw that the super contractor runs before instance variables get their default values, could any one explain that to me ? Example i used in case someone want to try it: package courseExercise; class test { test() { System.out.println("Super Constructor run"); } } public class Init extends test { private Integer i = 6

Best way of declaring private variables in cocoa

心不动则不痛 提交于 2019-12-18 04:23:30
问题 I would like to know what the recommendations are for declaring private instance variables in cocoa. This question is in the context of developing apps on the iPhone. I am aware of at least three ways of declaring private variables: Declare them in the interface of the h file with the modifier @private: @interface MyClass : NSObject { @private NSObject * myPrivateVar; } Declare them in the implementation section of the m file: @implementation MyClass NSObject * myPrivateVar; Declare a

Problem in instance variable initialization

坚强是说给别人听的谎言 提交于 2019-12-17 19:04:22
问题 Heres some sample code, class Base { private int val; Base() { val = lookup(); } public int lookup() { //Perform some lookup // int num = someLookup(); return 5; } public int value() { return val; } } class Derived extends Base { private int num = 10; public int lookup() { return num; } } class Test { public static void main(String args[]) { Derived d = new Derived(); System.out.println("d.value() returns " + d.value()); } } output: d.value() returns 0 // I expected 10 as lookup() is

Properties and Instance Variables in Objective-C

*爱你&永不变心* 提交于 2019-12-17 10:12:02
问题 I'm rather confused about properties and instance variables in Objective-C. I'm about half-way through Aaron Hillegass's "Cocoa Programming for Mac OS X" and everything is logical. You would declare a class something like this: @class Something; @interface MyClass : NSObject { NSString *name; NSArray *items; Something *something; IBOutlet NSTextField *myTextField; } @property (nonatomic, retain) NSString *name; @property (nonatomic, retain) NSArray *items; Since other objects need to

When do Ruby instance variables get set?

社会主义新天地 提交于 2019-12-17 10:08:17
问题 class Hello @hello = "hello" def display puts @hello end end h = Hello.new h.display I created the class above. It doesn't print anything out. I thought the instance variable @hello was set during the class declaration. But when I call the display method the output is 'nil'. What's the correct way to do this? 回答1: Instance variables in ruby may be a bit confusing when first learning Ruby, especially if you are accustomed to another OO language like Java. You cannot simply declare an instance