class-variables

Calling class variable with self

心已入冬 提交于 2019-12-19 04:29:21
问题 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_

C++ : Initializing base class constant static variable with different value in derived class?

一笑奈何 提交于 2019-12-18 06:14:15
问题 I have a base class A with a constant static variable a. I need that instances of class B have a different value for the static variable a. How could this be achieved, preferably with static initialization ? class A { public: static const int a; }; const int A::a = 1; class B : public A { // ??? // How to set *a* to a value specific to instances of class B ? }; 回答1: You can't. There is one instance of the static variable that is shared by all derived classes. 回答2: Static members are unique in

C++ : Initializing base class constant static variable with different value in derived class?

試著忘記壹切 提交于 2019-12-18 06:14:13
问题 I have a base class A with a constant static variable a. I need that instances of class B have a different value for the static variable a. How could this be achieved, preferably with static initialization ? class A { public: static const int a; }; const int A::a = 1; class B : public A { // ??? // How to set *a* to a value specific to instances of class B ? }; 回答1: You can't. There is one instance of the static variable that is shared by all derived classes. 回答2: Static members are unique in

Is Rails shared-nothing or can separate requests access the same runtime variables?

跟風遠走 提交于 2019-12-17 15:45:03
问题 PHP runs in a shared-nothing environment, which in this context means that every web request is run in a clean environment. You can not access another request's data except through a separate persistence layer (filesystem, database, etc.). What about Ruby on Rails? I just read a blog post stating that separate requests might access the same class variable. It has occurred to me that this probably depends on the web server. Mongrel's FAQ states that Mongrel uses one thread per request -

Python: Accessing class variables from other class variables within the class - possible?

旧城冷巷雨未停 提交于 2019-12-14 03:14:57
问题 is it possible in python to address class variables from other class variables within the same class? My problem is: I am trying to prepare some static code, that would look like this: class MyBaseObject: SIGNAL_NAME_1 = "signal-name-1" SIGNAL_NAME_2 = "signal-name-2" ALL_SIGNALS = { SIGNAL_NAME_1: ( signal-definition ), SIGNAL_NAME_2: ( signal-definition ) } My problem with the above is that, according to python SIGNAL_NAME_1 and _2 are not defined while creating dict. Accessing them by

Python: define object variable without initialization

折月煮酒 提交于 2019-12-13 11:10:08
问题 I am trying to rewrite my code from one big function to oop. If I have this, it crash on session.add(a1) # Unresolved reference : from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import * from sqlalchemy.orm import * Base = declarative_base() class Address(Base): __tablename__ = 'address' id = Column(Integer, primary_key=True) street = Column(String, nullable=False) city = Column(String, nullable=False) user = relationship('User', back_populates="address") class Main():

Counter variable for class

爱⌒轻易说出口 提交于 2019-12-12 07:42:52
问题 I am having problem getting this piece of code to run. The class is Student which has a IdCounter, and it is where the problem seems to be. (at line 8) class Student: idCounter = 0 def __init__(self): self.gpa = 0 self.record = {} # Each time I create a new student, the idCounter increment idCounter += 1 self.name = 'Student {0}'.format(Student.idCounter) classRoster = [] # List of students for number in range(25): newStudent = Student() classRoster.append(newStudent) print(newStudent.name) I

What's the difference between a class variable and a parameter in a constructor?

自作多情 提交于 2019-12-12 01:28:10
问题 I am asking this in response to this answer. I'm using an example of Variable and Var - edit Pls note I am asking where to use Var or Variable : Class NewClass { private String Variable = ""; Public Class (String Var) { NewClass.Var = Variable; } } OR private String Variable = ""; Public Class (String Variable) { NewClass.Variable = Var; // OR WHATEVER OTHER COMBINATIONS IT MAY BE. } } Which ones are the class variables, how does this differ from the parameters and which goes where? edit I

Get static variable value

荒凉一梦 提交于 2019-12-11 18:00:58
问题 I'm trying to create a static variable to be accessed through different classes, assigning value to it, and getting this value when needed. I did use this way in order to achieve this, and that leads me to including a property as following: class GetPartition(Partition): _i = 100 def __init__(self): super(Partition,self).__init__("get") def get_i(self): return type(self)._i def set_i(self,val): type(self)._i = val i = property(get_i, set_i) and this is class Partition if needed: class

What is the proper way to access python class variables?

Deadly 提交于 2019-12-11 17:48:49
问题 Having a class class A(object): z = 0 def Func1(self): return self.z def Func2(self): return A.z Both methods ( Func1 and Func2 ) give the same result and are only included in this artificial example to illustrate the two possible methods of how to address z . The result of Func* would only differ if an instance would shadow z with something like self.z = None . What is the proper python way to access the class variable z using the syntax of Func1 or Func2 ? 回答1: I would say that the proper