Tagged Questions
34
votes
8answers
33k views
Python - Get Instance Variables
Is there a built-in method in Python to get an array of all a class' instance variables? For example, if I have this code:
class hi:
def __init__(self):
self.ii = "foo"
self.kk = "bar"
Is ...
6
votes
4answers
192 views
Should internal class methods returnvalues or just modify instance variables in python?
I am creating a query builder class that will help in constructing a query for mongodb from URL params. I have never done much object oriented programming, or designed classes for consumption by ...
5
votes
4answers
1k views
Constant instance variables?
I use 'property' to ensure that changes to an objects instance variables are wrapped by methods where I need to.
What about when an instance has an variable that logically should not be changed? Eg, ...
4
votes
2answers
170 views
Why doesn't Python use C++/Java-like syntax to define instance variables?
This plagued me for hours, as I am from the C++ world. I finally found out what was going on, but I do not know why this is the default behaviour. I'd like to understand why the language is designed ...
4
votes
2answers
64 views
Python adding element to an instance's list also adds it to another instance's list [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I've created a Person class which has a name and a list of children. The children are of the same Person ...
3
votes
4answers
339 views
class variables is shared across all instances in python?
I started coding in python a week ago, it is my mistake i started coding using oops,classes and objects that soon. I assumed my C++ proficiency will help.... I got bit by the following code
class A:
...
3
votes
2answers
379 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' ...
3
votes
3answers
95 views
Calling an instance variable's method from the instance itself
I have a situation where I want be able to call a commonly used method of an instance variable from the instance itself. This isn't really necessary for the program to function; however, it's much ...
2
votes
2answers
64 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 ...
2
votes
1answer
150 views
In python __init__ dictionary arguments defaulting to previous class values [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm finding that dictionary arguments to the init() function of my class are defaulting to ...
2
votes
1answer
140 views
Defining __repr__(self) to be created dynamically in python
I have a class
class FooBar(object):
def __repr__(self):
pass
and I want to implement the __repr__ function. Since I am using FooBar as sort of a handy container and I add some ...
1
vote
7answers
2k views
What is the correct (or best) way to subclass the Python set class, adding a new instance variable?
I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of ...
1
vote
1answer
88 views
Trouble calling instance variables in Python
I'm just starting to learn my way around classes now and I came across something I don't understand. Let's say I have a class...
class Area(object):
def __init__(self, name, items):
...
1
vote
3answers
295 views
Python Eclipse type casting intellisense work-around
Say I have the following two classes.
class TopClass:
def __init__(self):
self.items = []
class ItemClass:
def __init__(self):
self.name = None
And I want to use that in the ...
1
vote
3answers
274 views
using variables in class functions in another class (python)
I want to use the variables i have declared inside a function in one class, in another class.
For example i want to use the variable "j" in another class. Is it possible? (I read somewhere that it ...