class-variables

how to create class variable dynamically in python

若如初见. 提交于 2019-12-02 19:21:52
I need to make a bunch of class variables and I would like to do it by looping through a list like that: vars=('tx','ty','tz') #plus plenty more class Foo(): for v in vars: setattr(no_idea_what_should_go_here,v,0) is it possible? I don't want to make them for an instance (using self in the __init__) but as class variables. You can run the insertion code immediately after a class is created: class Foo(): ... vars=('tx', 'ty', 'tz') # plus plenty more for v in vars: setattr(Foo, v, 0) Also, you can dynamically store the variable while the class is being created: class Bar: locals()['tx'] =

Java: Getting the properties of a class to construct a string representation

一曲冷凌霜 提交于 2019-12-02 19:05:36
Let's say I have a class like this (and also further assume that all the private variables: public class Item { private String _id = null; private String _name = null; private String _description = null; ... } Now, if I want to build a toString() representation of this class, I would do something like this inside the Item class: @Override public String toString() { return (_id + " " + _name + " " + _description); } But what if I have say 15 private variables inside the class? Do I have to write the name of each and every variable like this? Ideally, I would like to get over with the task by

Is final ill-defined?

浪尽此生 提交于 2019-12-02 14:15:43
First, a puzzle: What does the following code print? public class RecursiveStatic { public static void main(String[] args) { System.out.println(scale(5)); } private static final long X = scale(10); private static long scale(long value) { return X * value; } } Answer: 0 Spoilers below. If you print X in scale(long) and redefine X = scale(10) + 3 , the prints will be X = 0 then X = 3 . This means that X is temporarily set to 0 and later set to 3 . This is a violation of final ! The static modifier, in combination with the final modifier, is also used to define constants. The final modifier

Class variables, scope resolution operator and different versions of PHP

女生的网名这么多〃 提交于 2019-12-02 10:03:11
I tried the following code in codepad.org: class test { const TEST = 'testing 123'; function test () { $testing = 'TEST'; echo self::$testing; } } $class = new test; And it returned with: 1 2 Fatal error: Access to undeclared static property: test::$testing on line 6 I want to know whether referencing a class constant with a variable would work on my server at home which runs php 5.2.9 whereas codepad uses 5.2.5 . What are changes in class variables with each version of PHP? The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that

Class Variable behaving like Instance Variable (Python 3.4)

江枫思渺然 提交于 2019-12-02 08:54:52
问题 Python 3.4.0a1 Windows 8.1 Class Created: class Bank(object): bankrupt = False Command entered in IDLE __main__ with the following results: >>> a = Bank() >>> b = Bank() >>> a.bankrupt False >>> b.bankrupt False >>> b.bankrupt = True >>> b.bankrupt True >>> a.bankrupt False Expected output: I expected a.bankrupt to change to True when I changed b.bankrupt since the variable bankrupt is defined for the entire class and not for a single instance (with self.bankrupt ) Why is this not happening?

Class Variable behaving like Instance Variable (Python 3.4)

你。 提交于 2019-12-02 06:20:16
Python 3.4.0a1 Windows 8.1 Class Created: class Bank(object): bankrupt = False Command entered in IDLE __main__ with the following results: >>> a = Bank() >>> b = Bank() >>> a.bankrupt False >>> b.bankrupt False >>> b.bankrupt = True >>> b.bankrupt True >>> a.bankrupt False Expected output: I expected a.bankrupt to change to True when I changed b.bankrupt since the variable bankrupt is defined for the entire class and not for a single instance (with self.bankrupt ) Why is this not happening? You assigned to a new attribute to the instance instead. To change a class attribute, assign directly

instance variable, class variable and the difference between them in ruby

痞子三分冷 提交于 2019-12-01 09:04:55
I am having a hard time understanding instance variable, class variable and the difference between them in ruby... can someone explain them to me? I have done tons of Google searches, just can't understand them fully. Thank you! Let's say you define a class. A class can have zero or more instances. class Post end p1 = Post.new p2 = Post.new Instance variables are scoped within a specific instance. It means if you have an instance variable title , each post will have its own title. class Post def initialize(title) @title = title end def title @title end end p1 = Post.new("First post") p2 = Post

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__

Python OOP and lists

我是研究僧i 提交于 2019-11-30 17:48:21
问题 I'm new to Python and it's OOP stuff and can't get it to work. Here's my code: class Tree: root = None; data = []; def __init__(self, equation): self.root = equation; def appendLeft(self, data): self.data.insert(0, data); def appendRight(self, data): self.data.append(data); def calculateLeft(self): result = []; for item in (self.getLeft()): if (type(item) == type(self)): data = item.calculateLeft(); else: data = item; result.append(item); return result; def getLeft(self): return self.data;

Constants or class variables in ruby?

江枫思渺然 提交于 2019-11-30 13:34:56
问题 I've been programming in Ruby for a few months now, and I'm wondering when it is appropriate to use constants over class variables and vice versa. (I'm working in Rails, thinking about constants in models). class Category TYPES = %w(listing event business).freeze end OR class Category @@types = %w(listing event business).freeze cattr_reader :types end Are there circumstances where one is preferable to another? Or is it just a matter of taste/style? 回答1: The main thing is that by using the