class-variables

Prevent / alter access to class variables [duplicate]

扶醉桌前 提交于 2019-12-05 03:13:19
This question already has answers here : Class-level read-only properties in Python (3 answers) Closed 3 years ago . Is there a way to prevent or alter access to class variables in Python as one can via overriding __setattr__ for instance variables? Note that this question is mistitled and actually refers to instance variables, not class variables. Based on reading multiple posts about the (apparent) deathtrap that is __slots__ , I'd prefer not to go that route (and I haven't looked into it enough to know if it does what I'm asking). Example: class A(object): foo = "don't change me" def _

Python: self vs type(self) and the proper use of class variables

无人久伴 提交于 2019-12-04 20:56:02
When using a class variable in Python, one can access and (if it's mutable) directly manipulate it via "self" (thanks to references) or "type(self)" (directly), while immutable variables (e.g., integers) apparently get shadowed by new instance objects when you just use "self". So, when dealing with Python class variables, is it preferable/Pythonic to simply always use "type(self)" for working with class variables referred to from within class methods? (I know class variables are sometimes frowned upon but when I work with them I want to access them in a consistent way (versus one way if they

Does Objective-C support class variables?

谁说胖子不能爱 提交于 2019-12-04 17:51:25
问题 I know it supports automatic variables, but what about class variables? 回答1: The language does not support class variables. You implement class-specific state with global static variables in the compilation units of the implementation. In the header (.h file): @interface MyClass : NSObject +(int)val; @end In the implementation (.m file): static int val = 123; @implementation MyClass +(int)val {return val;} @end Usage: if ([MyClass val] > 100) ... 回答2: ObjC class variables are plain old static

Why should @@class_variables be avoided in Ruby?

天涯浪子 提交于 2019-12-04 16:48:47
问题 I know that some say that class variables (e.g. @@class_var ) should be avoid in Ruby and should use the an instance variable (e.g. @instance_var ) in the class scope instead: def MyClass @@foo = 'bar' # Should not do this. @foo = 'bar' # Should do this. end Why is the use of class variables frowned upon in Ruby? 回答1: Class variables are often maligned because of their sometimes confusing behavior regarding inheritance: class Foo @@foo = 42 def self.foo @@foo end end class Bar < Foo @@foo =

Scope of class variable

守給你的承諾、 提交于 2019-12-04 08:02:48
Setting a class variable @@foo in two classes B and C , where neither is a subclass of the other but they both include a common module A , seems to create @@foo separately for B and C , which A cannot access: module A; end class B; include A; @@foo = 1 end class C; include A; @@foo = 2 end module A; p @@foo end # => NameError: uninitialized class variable @@foo in A class B; p @@foo end # => 1 class C; p @@foo end # => 2 But when @@foo is assigned in A , which works as an ancestor to both B and C , the @@foo that B and C access becomes the @@foo of A . module A; @@foo = 3 end class B; p @@foo

how to access a class variable of outer class from inner class in ruby

依然范特西╮ 提交于 2019-12-04 07:31:04
i have some code in Ruby here below: class A @@lock = Monitor.new class B def method @@lock.synchronize puts "xxxxx" end end end end after running it throws an error which said that below: uninitialized class variable @@lock in A::B (NameError) if i want to know how to access the outer class A's class variable @@lock from inner class B's method, how to do it? thank you in advance. The only way to access this class variable is via an accessor method class A def self.lock @@lock ||= Monitor.new end class B def method A.lock.synchronize puts "xxxxx" end end end end I don't think you can without

Example of something that is not thread-safe in Rails

六月ゝ 毕业季﹏ 提交于 2019-12-03 13:52:07
I've seen threads like this on thread-safety in Rails and various web pages on the topic, and I'm sure everyone's great at reciting what it is and giving 'tips' on what isn't thread-safe ("class variables!"), but I can never seem to find a clear, simple, complete example of something that is actually not thread-safe in Rails , to the point where I wonder if anyone actually understands it at all. I would be grateful if someone could prove me wrong and give: a clear, simple, complete example of something that is not thread-safe in Rails. It should be clear where the code is (i.e., if it is in

Does Objective-C support class variables?

≡放荡痞女 提交于 2019-12-03 10:41:38
I know it supports automatic variables, but what about class variables? dasblinkenlight The language does not support class variables. You implement class-specific state with global static variables in the compilation units of the implementation. In the header (.h file): @interface MyClass : NSObject +(int)val; @end In the implementation (.m file): static int val = 123; @implementation MyClass +(int)val {return val;} @end Usage: if ([MyClass val] > 100) ... ObjC class variables are plain old static variables. Foo.m : static int foo = 0; Alternatively, you can use a C++ anonymous namespace if

getting a dictionary of class variables and values

[亡魂溺海] 提交于 2019-12-03 02:26:47
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 will love if there are ideas on how to handle this problem just like dict can be used to set a class

Pass a class variable to another class

青春壹個敷衍的年華 提交于 2019-12-02 22:01:28
问题 I'd like to pass a class variable to another class and make it a class variable of that class. How would I do this in the following context? public class GLCamTest extends Activity { public float array[] = something; } class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer { //Get class variable here } 回答1: It is difficult to understand wjat you are asking, but here's a possible answer: Make class B a subclass of A: public class A { //