Tagged Questions
2
votes
2answers
17 views
How to call another Object's Method from the current Object's Method in Python
I'm pretty new to OOP.
I'm trying to simulate cars moving along a road in a graph-like fashion. Each Road object has a source and destination. When a car reaches the end of the road, I want the road ...
0
votes
1answer
24 views
Change choices attribute of model's field
This question is similar to Set model field choices attribute at run time?
However, my problem is that I want to change the default value of the choices attribute, at class level.
I have this
class ...
1
vote
2answers
57 views
Last element from a list using method
I am trying to obtain the last position from my list generated in the following way:
X = ['A','B','C']
Y = ['1','2','3']
class Combination:
def __init__(self,x,y):
if (x in X) and (y ...
2
votes
2answers
22 views
Getting value from a list of objects
I try to write a program which will generate me all possible combinations of positions in given range of X and Y. For example: [A1,A2,A3,B1,...]
As I am new to OOP I have some problem with printing ...
1
vote
1answer
33 views
Python, executing extra code at method definition
I am writing a python API/server to allow an external device (microcontroller) to remotely call methods of an object by sending a string with the name of the method. These methods would be stored in a ...
3
votes
2answers
34 views
Can one declare an abstract exception in Python?
I would like to declare a hierarchy of user-defined exceptions in Python. However, I would like my top-level user-defined class (TransactionException) to be abstract. That is, I intend ...
0
votes
0answers
23 views
ftplib reconnect connection
Edited this Question as Suggested:
I just got I little bit more informations about the problem.
When the reconnect fails it stuck right after the:
self.connect(self.hostip, self.port)
So the ...
0
votes
4answers
132 views
Bank account Classes. How to make it more powerful/efficient? (OOP)
I want to make this program work without using global variables. Basically, if the person withdraws and ends up going in negative figures, they incur a penalty of 5. Every transaction that's in minus ...
0
votes
1answer
70 views
Updating Objects Simultaneously
I have to create a program in python where I have to make thirty balls bounce around the screen using classes. I have created a class called "Ball" and I am trying to create a list of Ball and update ...
0
votes
1answer
28 views
Class scopes in Python Bottle routes
Inside a bottle route I am instantiating a class.
Feasibly, this page may get called simultaneously and need to create simultaneous instances of this class named "newuser" in the function.
I wanted ...
0
votes
1answer
37 views
how to pass argparse arguments to a class
I'm very new to coding in general and Python in particular. I'm trying to learn how to pass argparse arguments I have created into a class for use the right/recommend way. In addition to learning ...
1
vote
1answer
38 views
Best practice to add extra attributes for an object that related only to a specific class
I have the following class:
class Player(object):
def __init__(self, name, age):
self.name = name
self.age = age
I have a class(Lobby) that contain players.
The class should ...
-3
votes
1answer
35 views
Python Instance Variable not recognized [duplicate]
I'm working on this OOP program in Python
class SimpleString():
popSize = 1000 #Should be even number
displaySize = 5
alphatbet = "abcdefghijklmnopqrstuvwxyz "
def __init__(self):
...
5
votes
2answers
63 views
Why is Python class not recognizing static variable
I am trying to make a class in Python with static variables and methods (attributes and behaviors)
import numpy
class SimpleString():
popSize = 1000
displaySize = 5
alphatbet = ...
4
votes
1answer
92 views
Is it pythonic to separate implementation method to be overridden?
I found it seems useful to separate abstract method into two methods, one for public interface, the other to be overridden by subclasses.
This way you can add precondition / postcondition check for ...
0
votes
2answers
43 views
this constructor takes no arguments
I'd like to make a simple calculator in Tkinter. I've got few lines of code and want to check if it works. But then I recevie such error: "Calc(T.Tk()).run() this constructor takes no arguments". Here ...
0
votes
1answer
28 views
Call method in a class which called the current class
How could I call the method 'z'? Which is he best way for this?
And to an object?
File test1.py:
from test2 import Test2
class Test1(object):
def __init__(self):
pass
def a(self):
...
0
votes
2answers
43 views
Order of attributes in a class
In python is guaranteed that the order of attributes in a class is maintained? How this is guaranteed? Where in manual is specified?
This defines the fields declaratively as class attributes:
class ...
0
votes
3answers
34 views
obj.count = 8 or setattr(obj, 'count', 8)
In python. What is the difference between using the setattr(object, 'var', value and the object.var = value method. They both seem to do the same thing
class Something(object):
pass
x = ...
0
votes
1answer
28 views
The histogram 'Clone' method in PyROOT/rootpy
I am doing some plotting of ROOT histograms using rootpy and have run in to a bit of a strange problem.
I get a ROOT histogram using:
Histo1 = FilePure.Get(HistoName + str("incl") + str(0))
...
2
votes
1answer
30 views
Proper design to reuse the results of more general functions
Let's say we have a situation when we create a class which has some set of parameters and we can pull some data for an instance of that class (say, for some particular date) from a database (which is ...
0
votes
2answers
17 views
Attributes from superclass constructor in Python 3 on created or accessible?
I am getting this traceback:
Traceback (most recent call last):
File "/home/amentis/Dropbox/Rexi/index.py", line 21, in application
ui = Console.Console()
File ...
2
votes
0answers
43 views
basic python query. NameError in __init__ method
NameError: name 'the_shape' is not defined
I get the following error when I try to import my KMeans module containing the class named KMeansClass. The KMeans.py module has the following structure:
...
2
votes
1answer
99 views
Difference between Class variables and Instance variables
I have already read many answers here on Stack Exchange like Python - why use "self" in a class?
After reading these I understand that instance variables are unique to each instance of the ...
0
votes
1answer
31 views
Class instance attributes won't seem to initialize [closed]
This seems like such a simple question, but there don't seem to be any answers that address my particular issue, which is why the init method never actually initiates the class instance variable ...
0
votes
1answer
31 views
Import modules with class
Problem: I have got a script with a class and some functions in a folder (with __ init __ .py). In root folder I have got a main.py script.
In main script I called the module with the model:
import ...
0
votes
1answer
35 views
Python Trivia Game and reading from files
class Question(object):
def __init__(self, question, options, answer, description, points):
self.question = question
self.options = options
self.answer = answer
self.description = ...
2
votes
2answers
43 views
PyGame - Getting the size of a loaded image
Hello, even though you might think there was a similar question, mine is pretty different than this.
I am trying to load an image from a directory, and set my screen size (automatically) to the size ...
1
vote
3answers
59 views
Why isn't my program outputting anything in the console (using object-oriented programming in Python)? [closed]
Here is my code. There are two files one contain the Book class object and one the tests the object. This is all in python
book.py
class Book(object):
"""Represents a book in a library."""
...
0
votes
0answers
19 views
Python's Object is reusing optional argument generated on method call [duplicate]
Ok, here is my code:
class foo():
def x(self, arg = []):
print arg
if len(arg):
print "arg already contains data"
arg.append("one call were made")
Pretty ...
0
votes
2answers
41 views
Defining a python class “alias” within the class definition
I'd like to be able to assign aliases to class names, and somehow define the aliases within the class body. So, for example, rather than doing this:
class C(object):
pass
C1 = C
I'd like to be ...
1
vote
3answers
77 views
Python: Using OOP and accessing methods (self, other)
I want to draw locations for firm type 0 and 1 from the uniform distribution, and to calculate distances between the two types by using the OOP concept in Python.
class Firm_loc:
def ...
0
votes
0answers
32 views
Keeping track of class instances: class variable, dedicated collection, or inheritance?
I am building a socket-based interoperability wrapper for an existing .NET API. The users of the wrapper will organize their code in classes which will be instantiated by a message handler.
I want to ...
0
votes
2answers
70 views
How to protect class attributes in Python?
How to protect class from adding attributes in that way:
class foo(object):
pass
x=foo()
x.someRandomAttr=3.14
0
votes
1answer
39 views
Python: inheritance of class data (if superclass object has been already initialized)
I faced with an inability of the inheritance of superclass attribute values. I have already called superclass constructor and now trying to check out the inherited values.
class base:
def ...
-1
votes
1answer
36 views
Better to return data structure or instance?
This is software design question with (dummy) examples in Python but I'm guessing it's applicable to any language.
The basic question is: In a rather large project/library is it usually better to ...
2
votes
3answers
78 views
How to fix objects of wrong type in Python?
i am developing in Python 3, i have a method with an argument of my class Color, however when
i try to use a method:
def __init__(self, name, color=Color.Color("none"), style="solid", width="1px"):
...
0
votes
4answers
71 views
Do not understand this line of code in Python?
I am reading to some piece of code written by some experienced programmer, and I do not understand some part of it. Unfortunately, I am new to Python programming.
This is the line of code which ...
-3
votes
2answers
62 views
Creating an empty object in Python
Are there any shortcuts for defining an empty object in Python or do you always have to create an instance of a custom empty class?
Edit: I mean an empty object usable for duck typing.
0
votes
0answers
53 views
What to do if you're not satisfied with an API?
I am learning urwid.
Urwid listboxes have an API that doesn't suite me. For example, in order to change focus to the next/previous element, I would like to write :
listbox.focus_next() / ...
2
votes
3answers
58 views
Python - Is it possible to get the name of the chained function?
I'm working on a class that basically allows for method chaining, for setting some attrbutes for different dictionaries stored.
The syntax is as follows:
d = Test()
...
0
votes
1answer
66 views
alternative for passing references around in python
I'm relatively new to python and oop, and i have a question around the design of my code for a hobby project.
I created a lot of variables in my main program. These variables are lists of objects ...
4
votes
4answers
74 views
dynamic class with inheritance in python
How can I create classes dynamically in a class?
As I tried below, I get a NameError: name 'Foo is not defined.
I am quite new to Python, pls forgive me if it's obvious.
class Parent(object):
...
0
votes
3answers
85 views
Updating all objects in a class
I'm very new to using OOP in Python.
I have a problem to solve, that I was able to solve it partially. And sorry if I cannot make my question very clear.
If I create a object test of a class ...
1
vote
2answers
37 views
OOP instance updating
I just started With OOP and i am confused with this code-
class cartesianPoint:
pass
cp1 = cartesianPoint()
cp1.x = 1.0
cp1.y = 2.0
cp1
>
<__main__.cartesianPoint instance at ...
-1
votes
1answer
45 views
Assign default arguments to Python class members
I am trying to instantiate a class from a dict. In the class constructor, I assign default values to some class members if not given:
class Country(object):
def __init__(self, continent, country ...
5
votes
5answers
119 views
Should I use abstract classes?
I have read that this is the best way to implement an abstract class in Python (as opposed to the abc module):
class Animal(object):
def speak(self):
raise NotImplementedError()
class ...
0
votes
0answers
28 views
Pygame subset for android - app close in events
I am doing one biology project for my school and i decided to develop an Android app using Python. I have experience with pygame, i already developed one app once, but now i have a problem:
The ...
0
votes
2answers
85 views
Multiple constructors in python, using inheritance
I have a class AbstractDataHandle, whith his init method, and a class Classifier. I would like to have two constructors in Classifier, Java like. One inherited from it`s superclass, and one brand new.
...
0
votes
0answers
19 views
pygtk unkown signal name
I'v written a custom class DirectoryView like this:
import pygtk
pygtk.require("2.0")
import gtk
import gobject
from bin.utils.decs import scrollable_widget
from bin.widget import ...