class-variables

Difference between instance attributes and class attributes

做~自己de王妃 提交于 2019-12-11 15:12:28
问题 I'm trying to learn about the instance and class attributes in python. Then am a little confused: is this_obj.var an instance attribute or it belongs to the class attribute. The code is below class Myclass (object): var = 10 this_obj = Myclass() this_obj.somevar = 12 that_obj = Myclass() that_obj.somevar = 12 回答1: Instance and class attributes can be kind of confusing to understand at first. The best way of thinking about it is to pay attention to the name. Instance attributes are owned by

Issue trying to use a class's name in it's own Class Variable section

给你一囗甜甜゛ 提交于 2019-12-11 09:59:23
问题 I'm writing an app in Python and PyQt right now, and I've having a bit of an issue. This problem doesn't require an knowledge of PyQt itself, just that of static variables in python. I'm tyring to add some signals to a class, which will emit the instance of the class when the signal is tripped. What I have is something like this: class Foo(QObject): # ... # Signals updated = pyqtSignal(Foo) moved = pyqtSignal(Foo) # ... Python is giving me the error: NameError: name 'Foo' is not defined IIRC,

Create a new obj with deepcopy but new obj share variable with the old obj

淺唱寂寞╮ 提交于 2019-12-11 08:44:42
问题 I am dealing with some classes using pygraph module and when I use add_node() method, it always comes out 'node xxx already in graph'. So I try to use deepcopy() to create a new instance and have some problem with it: class test: _storage = [] def add_item(self,item): self._storage.append(item) def pop_item(self,item): return self._storage.pop() def __repr__(self): return '%s' %self._storage[:] if __name__ == '__main__': a1 = test() a1.add_item(3) a1.add_item(4) from copy import copy,deepcopy

Python beginner Class variable Error

雨燕双飞 提交于 2019-12-10 12:49:01
问题 this is my first question, so sorry... I'm a beginner with python and coding in general, and i wanted to create a class called 'Map' that would have the following class variables: class Map: height = 11 width = 21 top = [['#']*width] middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)] field = top + middle + top b = Map() Shell: >>> middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)] NameError: name 'width' is not defined If i put the variables outside of the class it

difference between class method , instance method , instance variable , class variable?

隐身守侯 提交于 2019-12-10 12:17:59
问题 I recently started learning ruby. I am confused between class methods, instance methods, instance variables, and class variables. I googled a lot, but I didn't get any clarification on those. Any help along with examples would be appreciated. 回答1: First take a look at this diagram: You can rightly say that “ obj has a method called my_method( ),” meaning that you’re able to call obj.my_method(). By contrast, you shouldn’t say that “MyClass has a method named my_method().” That would be

How to define a class variable on a singleton class

点点圈 提交于 2019-12-10 10:54:47
问题 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? 回答1: It is because when Ruby parser meets a class variable, the

Prevent / alter access to class variables [duplicate]

橙三吉。 提交于 2019-12-10 03:26:22
问题 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

Why is using a class variable in Ruby considered a 'code smell'?

[亡魂溺海] 提交于 2019-12-10 02:46:50
问题 According to Reek, creating a class variable is considered a 'code smell'. What is the explanation behind this? 回答1: As you can find in their documentation on Class Variables: Class variables form part of the global runtime state, and as such make it easy for one part of the system to accidentally or inadvertently depend on another part of the system. So the system becomes more prone to problems where changing something over here breaks something over there. In particular, class variables can

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

穿精又带淫゛_ 提交于 2019-12-10 00:14:46
问题 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

Scope of class variable

坚强是说给别人听的谎言 提交于 2019-12-09 18:29:35
问题 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