Tagged Questions
1
vote
2answers
38 views
Python Thread class variable is blank
I've been trying to fix this issue for the past few hours, and i just can't figure out what i'm doing wrong!
I have a single python file:
REFRESH_RATE=10.0
MAX_SECONDS=30
class user_manager:
...
0
votes
3answers
94 views
Python: self vs type(self) and the proper use of class variables
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., ...
1
vote
3answers
51 views
Potential uses of an empty tuple in Python
I was playing around with the risks of class-level variables in Python, and I thought that the risk of lists as class-level variables can be solved with tuples, for example, an empty tuple.
Take:
...
0
votes
1answer
21 views
Python class variable losing scope on assignment
I have a question regarding class variable scope persistence on assignment.
I have a dictionary as a class variablle, and as soon as I assign another dictionary to the variable in an instance method, ...
1
vote
3answers
37 views
Class Variable behaving like Instance Variable (Python 3.4)
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 = ...
2
votes
1answer
47 views
Defining class as enum using lot of memory
I have hundreds of classes with many properties in a file. I want to let the user know what are the different properties available for a particular class. My main aim is to support auto-completion ...
1
vote
2answers
29 views
Access local classvariable from inherited instance
I have my pseudo Interface, which I implement several times. Each implementation is supposed to store a variable that basically defines a path to a file (a template). Because these classes are ...
0
votes
2answers
19 views
Different class variables and usage
In a class in Python there are several ways to assign something to a variable.
I always don't know where I should do it, what is the difference in usage and in which scenario should I use a distinct ...
0
votes
1answer
62 views
Class object variable = none
I'm trying to declare a class variable which should first be set as a database object in the get function. Then when changes are made, the post function should change an attribute of the class ...
1
vote
3answers
52 views
Enforcing Class Variables in a Subclass
I'm working on extending the Python webapp2 web framework for App Engine to bring in some missing features (in order to make creating apps a little quicker and easier).
One of the requirements here ...
0
votes
3answers
170 views
getting a dictionary of class variables and values
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 ...
0
votes
3answers
55 views
Access class when used as class-level variable in Python
I do apologize if this question is already answered on SO or if my problem could be solved by a simple Google search, but I don't know the terminology to describe it, other than the question title, ...
1
vote
2answers
59 views
Create a new obj with deepcopy but new obj share variable with the old obj
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 ...
3
votes
1answer
59 views
Python keyword to access class variable
Consider the following class in Python 3:
class Foo(AnotherClass):
id_counter = 0
def __init__(self):
super().__init__()
self.id = Foo.id_counter
Foo.id_counter += 1
...
1
vote
4answers
79 views
Multiple instances of a class containing a list in python [duplicate]
When running following code in python, i create 2 objects of one class, that contains a list object:
class Test():
def run(self):
obj1 = klasse("Werner")
...
-1
votes
2answers
66 views
Python interpreter couldn't find the class variable [duplicate]
I am trying to create a distributed hash table. There is a thread. But the run function in the thread cant find the sock variable which I am initializing in the constructor.
Here is the code -
from ...
2
votes
5answers
289 views
Dynamically Create Static Variables (Enum hack)
I'm trying to create a set of states for a Node class. Normally, I would do this by setting each Node instance's state variable to an int, and document which int corresponds to which state (since I ...
0
votes
3answers
627 views
Python Class Inheritance, __init__ and cls
The desired output of the code is that I have a class variable Team.stuff which has one entry holding the b instance, and the Player.stuff variable should be empty. Instead I get an error...
class ...
0
votes
1answer
51 views
Instance variable of type list behaves as class variable
In the following code, I thought list wouldbe a unique variable to each object constructed. Why is it shared as a class variable?
01 class Thing(object):
02 def __init__(self, my_list=[]):
03 ...
0
votes
5answers
102 views
Python : Behavior of class and instance variables
I have following two code samples
Example 1:
class MyClass(object):
def __init__(self, key, value):
self._dict = self._dict.update({key:value})
m = MyClass('ten',10)
print m._dict
...
0
votes
2answers
49 views
Class variables, instance variables and inheritance
class C2:
x = 2
z = 2
class C3:
w = 3
z = 3
def __init__(self):
self.w += 1
class C1(C2, C3):
x = 1
y = 1
I1 = C1()
I1.name = 'I1'
I2 = C1()
I2.name = 'I2'
print I1.w # 4
...
0
votes
2answers
75 views
how to view class variables from an inner class in python
I have the following class with inner class and class variable:
class MyForm(forms.ModelForm):
type_constant = 'type'
class Meta:
model = Customer
fields = ('type')
I ...
0
votes
1answer
100 views
Python defines my class twice on the same thread
I had assumed that in Python if I do some
class A:
print("hi")
this "hi" would only ever be printed more than once if I explicitly deleted A with some del A
In my slightly bigger project I ...
1
vote
3answers
2k views
How to access a class variable from another class in python?
I have the following code segment :
class A:
def __init__(self):
self.state = 'CHAT'
def method1(self):
self.state = 'SEND'
def printer(self):
print self.state
...
-2
votes
1answer
69 views
Access class variable from outside the class
I want to access a variable I have defined in a class (as a class variable):
class Name(object):
Name.myVar = 0
The class has a few other functions and is saved in a module (mymodule).
Now I ...
1
vote
1answer
100 views
Python's Class Variables and Inheritance
I have here some code for a unit conversion program; it throws a NameError, because of Python's inheritance order.
class _Units :
_metric_unit_names = {'metric'}
_standard_unit_names = ...
2
votes
2answers
113 views
Significance and use of instance/class attributes with leading double underscores(special behaviour) [duplicate]
Possible Duplicate:
What is the benefit of private name mangling in Python?
While I was playing with python, I found that if class or instance variable-name starts with 2 underscores, they ...
0
votes
3answers
136 views
What is the proper way to access python class variables?
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 ...
0
votes
2answers
103 views
Class variables lookup vs instance variables lookup
I have a class which contains a number of class variables:
c_dict1, c_dict2, c_dict3
They are all dictionaries, and store some values using some of the instance variables below as keys.
I have a ...
4
votes
2answers
339 views
Prevent / alter access to class variables
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 ...
3
votes
2answers
142 views
Can I remove an inherited meta class from a python class?
E.g. is this possible?
class Foo(object):
class Meta:
pass
class Bar(Foo):
def __init__(self):
# remove the Meta class here?
super(Bar, self).__init__()
4
votes
3answers
135 views
Reference class variable in a comprehension of another class variable
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 ...
1
vote
2answers
285 views
Python: Accessing static class variables from inside the class
So I'm trying to make a class that extends list, with the extra ability that certain special attributes are mapped to refer to certain parts of the list. Using this Py3k doc page, I created the ...
2
votes
6answers
304 views
Python weird class variables usage
Suppose we have the following code:
class A:
var = 0
a = A()
I do understand that a.var and A.var are different variables, and I think I understand why this thing happens. I thought it was just ...
0
votes
3answers
91 views
static class variables
I can't understand how works the code below:
class Host:
name = None
fileList = []
def __init__(self, hostName):
self.name = hostName
def AddInfo(self,file):
...
0
votes
1answer
118 views
Python: Accessing class variables from other class variables within the class - possible?
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 ...
1
vote
3answers
155 views
Python - appending to class-level lists in derived class definitions
class A (object):
keywords = ('one', 'two', 'three')
class B (A):
keywords = A.keywords + ('four', 'five', 'six')
Is there any way to change A.keywords to <thing B derives ...
2
votes
1answer
1k views
Counter variable for class
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 ...
3
votes
2answers
3k views
Python Class Variable Initialization
I'd like to store some information about a class as class (static) variables. However, I can't figure out how these things get initialized. Here is a basic, dumb example:
class A(object):
clsVar ...
8
votes
2answers
2k views
how to create class variable dynamically in python
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:
...
1
vote
3answers
942 views
python refer to class variable while passing arguments to init
What I want to achieve:
1. Have a class variable keeping count of number of objects created
2. That variable should not be available to objects/others i.e. private to the class
3. If specific ID is ...
3
votes
2answers
445 views
Class variable and instance variable question in Python
When I have this class, the variable 'value' is class variable.
class Hello:
value = 10
def __init__(self):
print 'init'
I have an object 'h' and I could get the same value of '10' ...
1
vote
2answers
479 views
Transferring variables between classes
I have 2 classes that have the same variables but do the inverses of each other. is there a way to transfer the variables between them without hard coding every exchange or combining all the values ...
1
vote
1answer
225 views
storing passwords in class variables in python
I'm working on a python script that stores ssh passwords only during the current session. What I'm doing is declaring a class variable credentials = {}. When the script needs access to a specific ...
16
votes
2answers
11k views
python subclass access to class variable of parent
I was surprised to to learn that a class variable of a subclass can't access a class variable of the parent without specifically indicating the class name of the parent:
>>> class A(object):
...
2
votes
3answers
304 views
Python OOP and lists
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 ...
4
votes
1answer
319 views
Python Class Variables Question
I have some doubt about python's class variables. As my understanding, if I define a class variable, which is declared outside the __init__() function, this variable will create only once as a static ...
3
votes
1answer
200 views
What's the equivalent of Ruby's class @@variable in Python?
In Ruby 1.9, I can use its class variable like the following:
class Sample
@@count = 0
def initialize
@@count += 1
end
def count
@@count
end
end
sample = Sample.new
puts ...
2
votes
2answers
404 views
referencing static methods from class variable
I know it's wired to have such a case but somehow I have it:
class foo
#static method
@staticmethod
def test():
pass
# class variable
c = {'name' : <i want to reference test method ...
531
votes
12answers
283k views
Static class variables in Python
Is it possible to have static class variables or methods in python? What syntax is required to do this?