class-variables

getting a dictionary of class variables and values

人走茶凉 提交于 2019-12-09 04:44:56
问题 I am working on a method to return all the class variables as keys and values as values of a dictionary , for instance i have: first.py class A: a = 3 b = 5 c = 6 Then in the second.py i should be able to call maybe a method or something that will return a dictionary like this import first dict = first.return_class_variables() dict then dict will be something like this: {'a' : 3, 'b' : 5, 'c' : 6} This is just a scenario to explain the idea, of course i don't expect it to be that easy, but i

How to access a class variable from the outside in ruby?

天涯浪子 提交于 2019-12-08 19:15:42
问题 I'm trying to access a class variable from a method outside of the class. This is my class: class Book @@bookCount = 0 @@allBooks = [] def self.allBooks @@allBooks end def self.bookCount @@bookCount end attr_accessor :name,:author,:date,:genre,:rating def initialize(name, author, date, genre, rating) @name = name @author = author @date = date @genre = genre @rating = rating @@bookCount += 1 @@allBooks << self end end This is the method trying to access the class variable @@bookCount def

Get a static property of an instance

爷,独闯天下 提交于 2019-12-08 14:41:34
问题 If I have an instance in PHP, what's the easiest way to get to a static property ('class variable') of that instance ? This $classvars=get_class_vars(get_class($thing)); $property=$classvars['property']; Sound really overdone. I would expect $thing::property or $thing->property EDIT: this is an old question. There are more obvious ways to do this in newer PHP, search below. 回答1: You need to lookup the class name first: $class = get_class($thing); $class::$property $property must be defined as

Static class variables in Python — Lists & Objects [duplicate]

我的未来我决定 提交于 2019-12-08 06:45:58
问题 This question already has answers here : How to avoid having class data shared among instances? (7 answers) Closed 4 years ago . I'm new to Python, with more of a Java background. I understand the concept of static class variables in Python, but it's come to my attention that lists and objects don't work the same way a string would, for example - they're shared between instances of a class. In other words: class box (): name = '' contents = [] def __init__ (self, name): self.name = name def

Difference Between Dot Notation and -> in Objective C

十年热恋 提交于 2019-12-07 09:44:22
问题 I'm trying to use as little memory as possible in my code. I've tried two ways of sending a custom class object to a method. I'm not sure if there is any difference between these two approaches. Say I have 2 classes, Class1 and Class2 each with their own class variables and methods of course. All code is written in Class1 Approach 1: Class2 *class2Object = [[Class2 alloc] init]; [self doSomething: class2Object]; [class2Object release]; -(void) doSomething: (Class2 *) var { int a = var.a; }

How to define a class variable on a singleton class

倾然丶 夕夏残阳落幕 提交于 2019-12-06 12:48:12
I want to define a class variable on a singleton class. I checked this program's result: class C class << self @@val = 100 end end C.singleton_class.class_variables #=> [], I expect [:@@val] C.class_variables #=> [:@@val] I expect the scope of @@val to be the singleton class, isn't it? Would you tell me how to define a class variable on a singleton class using class << self , and the reason why this program is not correct? It is because when Ruby parser meets a class variable, the current class is resolved according to the lexical scope. Cf. http://blog.honeybadger.io/lexical-scoping-and-ruby

How a static variable is accessible before the declaration?

喜你入骨 提交于 2019-12-06 04:52:03
问题 public class Main { static int x = Main.y; // static int x = y; //Not allowed; y is not defined static int y = x; public static void main(String[] args) { System.out.println(x);//prints 0 } } How come I am allowed to use y trough the class, but not directly? When is y defined? 回答1: The precise rules governing forward reference to class variables are described in the section §8.3.2.3 of the JLS: 8.3.2.3 Restrictions on the use of Fields during Initialization The declaration of a member needs

Reference class variable in a comprehension of another class variable

落爺英雄遲暮 提交于 2019-12-06 01:47:10
问题 This may be a simple question, but I'm having trouble making a unique search for it. I have a class that defines a static dictionary, then attempts to define a subset of that dictionary, also statically. So, as a toy example: class example(object): first_d = {1:1,2:2,3:3,4:4} second_d = dict((k,first_d[k]) for k in (2,3)) This produces NameError: global name 'first_d' is not defined How should I be making this reference? It seems this pattern works in other cases, eg: class example2(object):

Difference Between Dot Notation and -> in Objective C

℡╲_俬逩灬. 提交于 2019-12-05 13:14:28
I'm trying to use as little memory as possible in my code. I've tried two ways of sending a custom class object to a method. I'm not sure if there is any difference between these two approaches. Say I have 2 classes, Class1 and Class2 each with their own class variables and methods of course. All code is written in Class1 Approach 1: Class2 *class2Object = [[Class2 alloc] init]; [self doSomething: class2Object]; [class2Object release]; -(void) doSomething: (Class2 *) var { int a = var.a; } Approach 2: Class2 *class2Object = [[Class2 alloc] init]; [self doSomething: &class2Object];

Observing a value of a static var in a class?

北城余情 提交于 2019-12-05 12:47:22
I have a class with a static var where the current online connection status is stored. I want to observe the value of ConnectionManager.online through other classes. I wanted to do this with KVO , but declaring a static variable as dynamic causes an error: class ConnectionManager: NSObject { dynamic static var online = false // adding 'dynamic' declaration causes error: // "A declaration cannot be both 'final' and 'dynamic' } What is a most elegant way of doing this? Update . This my code for the KVO part: override func viewDidLoad() { super.viewDidLoad() ConnectionManager.addObserver( self,