class-variables

python subclass access to class variable of parent

被刻印的时光 ゝ 提交于 2019-11-26 13:02:09
问题 I was surprised to to learn that a class variable of a subclass can\'t access a class variable of the parent without specifically indicating the class name of the parent: >>> class A(object): ... x = 0 ... >>> class B(A): ... y = x+1 ... Traceback (most recent call last): File \"<stdin>\", line 1, in <module> File \"<stdin>\", line 2, in B NameError: name \'x\' is not defined >>> class B(A): ... y = A.x + 1 ... >>> B.x 0 >>> B.y 1 Why is it that in defining B.y I have to refer to A.x and not

What does @@variable mean in Ruby?

感情迁移 提交于 2019-11-26 06:12:46
问题 What are Ruby variables preceded with double at signs ( @@ )? My understanding of a variable preceded with an at sign is that it is an instance variable, like this in PHP: PHP version class Person { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } Ruby equivalent class Person def set_name(name) @name = name end def get_name() @name end end What does the double at sign @@ mean, and how does it differ from a single at sign

Ruby class instance variable vs. class variable

孤街醉人 提交于 2019-11-26 00:50:29
问题 I read \"When do Ruby instance variables get set?\" but I\'m of two minds when to use class instance variables. Class variables are shared by all objects of a class, Instance variables belong to one object. There\'s not much room left to use class instance variables if we have class variables. Could someone explain the difference between these two and when to use them? Here\'s a code example: class S @@k = 23 @s = 15 def self.s @s end def self.k @@k end end p S.s #15 p S.k #23 I understand

Difference between class variables and class instance variables?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:45:19
Can anyone tell me about the difference between class variables and class instance variables? Wayne Conrad A class variable ( @@ ) is shared among the class and all of its descendants. A class instance variable ( @ ) is not shared by the class's descendants. Class variable ( @@ ) Let's have a class Foo with a class variable @@i , and accessors for reading and writing @@i : class Foo @@i = 1 def self.i @@i end def self.i=(value) @@i = value end end And a derived class: class Bar < Foo end We see that Foo and Bar have the same value for @@i : p Foo.i # => 1 p Bar.i # => 1 And changing @@i in one

Are static class variables possible in Python?

旧巷老猫 提交于 2019-11-25 23:56:02
问题 Is it possible to have static class variables or methods in Python? What syntax is required to do this? 回答1: Variables declared inside the class definition, but not inside a method are class or static variables: >>> class MyClass: ... i = 3 ... >>> MyClass.i 3 As @millerdev points out, this creates a class-level i variable, but this is distinct from any instance-level i variable, so you could have >>> m = MyClass() >>> m.i = 4 >>> MyClass.i, m.i >>> (3, 4) This is different from C++ and Java,

Ruby class instance variable vs. class variable

强颜欢笑 提交于 2019-11-25 23:43:52
I read " When do Ruby instance variables get set? " but I'm of two minds when to use class instance variables. Class variables are shared by all objects of a class, Instance variables belong to one object. There's not much room left to use class instance variables if we have class variables. Could someone explain the difference between these two and when to use them? Here's a code example: class S @@k = 23 @s = 15 def self.s @s end def self.k @@k end end p S.s #15 p S.k #23 I understand now, Class Instance Variables are not passed along the inheritance chain! Instance variable on a class:

What is the difference between up-casting and down-casting with respect to class variable

浪尽此生 提交于 2019-11-25 23:32:57
问题 What is the difference between up-casting and down-casting with respect to class variable? For example in the following program class Animal contains only one method but Dog class contains two methods, then how we cast the Dog variable to the Animal Variable. If casting is done then how can we call the Dog\'s another method with Animal\'s variable. class Animal { public void callme() { System.out.println(\"In callme of Animal\"); } } class Dog extends Animal { public void callme() { System