Tagged Questions
1
vote
3answers
39 views
Using Python eval to evaluate classes
How I can use eval to made class definitions inside other class ?
evalstr = str("class MyScreen(Screen):\n\tpass\n")
eval(evalstr)
I want to execute this code in other class method. But it returns ...
0
votes
1answer
21 views
Flask helper classes and return values
Greetings,
I am working with Flask and trying to create some helper classes instead of doing the data logic in the "controllers"/"routes", but my problem is that it doesn't seem to return anything ...
-1
votes
0answers
19 views
Python search in a generic n-nary tree's children nodes created with a class
I tried to make a generic n-nary tree in python like this -
class root:
def __init__(self):
self.children=[]
def setData(self,data):
self.mimeType=data.mimeType
...
-1
votes
1answer
32 views
Parent methods which return child class instances
Is it bad practice, and if so, why, to return instances of child classes as output of methods of an instance of the parent class?
For example, can the following --
class Parent(object):
def ...
0
votes
1answer
29 views
Python: Changes in class instance are not kept after the end of a function
I have this code structure:
class1 = Class1()
class2 = Class2(class1)
class2 has a method called callback, that receives commands via TCP, and depending on them, it tries to change stuff on ...
0
votes
1answer
21 views
Connecting a method of an imported class with a class of the main script
I couldn't search for these problem because I don't know the key word to use, therefore if it is duplicate or answered elsewhere, please let me know.
I have a file which creates the main window with ...
-4
votes
1answer
31 views
How can I redesign my code to avoid nested classes in python?
Suppose I have three classes: Outer, Inner1 and Inner2. Inner1 and Inner2 both need to access
data members from Outer, but apparently it isn't possible to access data from Outer if Inner1 and Inner2 ...
-1
votes
1answer
45 views
Efficient use of a dictionary for similar cases in Python
I am using Python in another program that sends and receives many similar messages. Essentially, the program sends an "address" and a "value". To be as concise as possible, I made a dictionary to ...
-2
votes
1answer
46 views
Python all objects changed if one is [closed]
I'm having a peculiar problem with values inside an object being set to whatever I set that variable to in later new instances. Let me try to explain first with an example of the kind of results I'm ...
-2
votes
1answer
26 views
Python inheritance confusion
I have a class Query:
class Query
def __init__(self, keyword, **kwargs):
self.keyword = keyword
self.parameters = kwargs
def __repr__(self):
return "Query keyword %s, ...
0
votes
1answer
15 views
object instance confusion in python
Hi I am trying to manipulate csv file as below
import csv
data=csv.reader(open('data.csv', 'rb'))
a= [row for row in data]
print a
The output of the code is (comma spearated rows--> in list ...
1
vote
1answer
50 views
How to present a class as a function?
As it was unclear earlier I am posting this scenario:
class Scraper:
def __init__(self,url):
self.start_page = url
def parse_html(self):
pass
def get_all_links(self):
...
0
votes
2answers
40 views
Meteor modularity and access protection — is everything 'global'?
I'm exploring Meteor and I'm trying to understand how it works, modularity-wise, compared to other more OOP systems I'm more familiar with, such as Python.
It seemed to me that breaking things ...
0
votes
1answer
22 views
Dynamically modifying attributes of an instance
I have my problem solved, but I would like to know why.
My situation is as following:
I defined the class Vector as:
class Vector:
def __init__(self, x=0, y=0):
self.x = x
...
2
votes
3answers
43 views
PySide: How to overload classes with static functions
I want to overload QInputDialog because I want to replace its QCombobox with my own QCombobox derivative. I checked the source-code of QInputDialog and tried to overload its ensureCombobox(). However, ...
1
vote
1answer
42 views
Why isn't callback being fired?
I'm fairly new to Python and currently I'm running a few examples to understand how things work, but I got stuck in one of the steps.
I have a class with several attributes in it, and an ...
0
votes
0answers
20 views
Prohibit deletion/creation/modification of properties in python [duplicate]
Is it possible to prohibit users from adding new properties by "appending" them to a class. e.g. MyClass.mynewproperty = 10
Similarly, is it possible to prohibit users from deleting properties ...
0
votes
3answers
37 views
Inherit variable from superclass
Let's assume I have two classes:
#!/usr/bin/env python
class First():
glob = 'Global data'
def __init__(self, arg1, arg2):
self.arg1 = arg1
...
0
votes
1answer
39 views
Can I provide Python class method as a parameter?
I would like to provide a method of a class as a parameter. Below is a simple example of what I'm after.
Say I have some class defined like this:
class TestClass(object):
def __init__(self):
...
2
votes
3answers
51 views
Is this Python example still initializing an object (without __init__)
I am still learning the concepts of OOP so please forgive me if this is a poor question:
If __init__ is needed to instantiate an object in Python, why does the following work with no call to ...
0
votes
0answers
29 views
How do I handle a database response as a list of objects?
I am making a SQL query like
"SELECT * FROM people"
in my python script and then the response is a list with a lot of people. I would like to wrap each people as an instance of my people-class and ...
0
votes
1answer
40 views
Python scope doubts
Some conceptual doubts. In this code:
class A:
print "here in A"
print "flying 1"
def main():
print "here in main"
print "flying 2"
if __name__ == "__main__":
main()
print "another ...
1
vote
2answers
37 views
Python, how can I define an object O such that func(O) = func(O.val) for any func
How can I define an object such that any function acting on it will act on one of its field? Is this possible?
class Mydata(object):
def __init__(self, val):
self.val = val
def ...
-1
votes
2answers
43 views
Error when trying to do a function in an OOP class
I am using the "More Python Programming for Beginners" book and I am copying code from it.
I am trying to create a function to create text on the screen called print_text() with a bunch of arguments ...
3
votes
1answer
47 views
__add__ matrices method in python 2.7
I'm newbee in Python, so i need your help.
Programm must add and substract random matrices.
import random
class Matrix:
def __init__(self):
self.mat = [[]]
def ...
-1
votes
3answers
37 views
Access global variable from external class
How to structure an application so global variables work ?
I'm looking for the simplest way to do something like :
main.py
from Class_A import Class_A
my_var = 3
c_a = Class_A()
print ...
2
votes
3answers
53 views
Beginner OOP; More methods vs Classes
I'm learning Python and trying to get into classes. I'm trying to scrape some sites that often have different strings I use to identify the info I need. The challenge is that I get into situations ...
1
vote
1answer
33 views
instance variable being changed unexpectedly
Trying to recurse through directories to find files that are not in both orig and dest directories. When I recurse into directories, I pass along a previous directory variable so that I know the ...
1
vote
2answers
28 views
Python: Attribute Error When Passing Method as Function Argument
I was sure that there'd be an answer to this question somewhere on stack overflow, but I haven't been able to find one; most of them are in regards to passing functions, and not methods, as arguments ...
0
votes
2answers
40 views
Allow subclass in a class to access base class properties
I'm trying to get my head around classes in Python and wonder if someone could help me solve the following problem please?
I have a base class which I intend to contain all information about a ...
1
vote
1answer
34 views
Elegant Solution to an OOP Issue involving type-changing in python
I run into an OOP problem when coding something in python that I don't know how to address in an elegant solution. I have a class that represents the equation of a line (y = mx + b) based on the m and ...
1
vote
1answer
38 views
Passing values to class
I have this abstract class in Python:
class TransactionIdsGenerator(object):
def getId(self):
raise NotImplementedError
And this class that implements:
class ...
2
votes
2answers
45 views
Python multiple inheritance constructor not called when using super()
Consider the following code:
class A(object):
def __init__(self):
pass
class B(object):
def __init__(self):
self.something = 'blue'
def get_something(self):
return ...
5
votes
1answer
73 views
Why am I allowed pickle instancemethods that are Theano functions, but not normal instancemethods?
In the process of using joblib to parallelize some model-fitting code involving Theano functions, I've stumbled across some behavior that seems odd to me.
Consider this very simplified example:
from ...
2
votes
4answers
50 views
Python OOP: inefficient to put methods in classes?
I usually use classes similarly to how one might use namedtuple (except of course that the attributes are mutable). Moreover, I try to put lengthy functions in classes that won't be instantiated as ...
6
votes
3answers
79 views
Method overloading for different argument type in python
I'm writing a preprocessor in python, part of which works with an AST.
There is a render() method that takes care of converting various statements to source code.
Now, I have it like this ...
2
votes
1answer
74 views
Python: OOP overhead?
I've been working on a real-time application and noticed that some OOP design patterns introduce incredible overheads in Python (tested with 2.7.5).
Being straightforward, why do simple accessor ...
1
vote
3answers
40 views
How to get a class's name from the object?
I have one function in my script where in the end I'm getting the class object. I want to return a class name from that function. What is the way to get classname from the respective class object?
1
vote
1answer
72 views
Python local variables in methods
I am learning Python 3 and I have very fundamental question regarding object oriented programming in Python. Here is my code.
class pet:
number_of_legs = 0
def count_legs(self):
...
1
vote
1answer
34 views
Data security of an instance after passing it to another instance in Python
I create an instance of class A, objA1 whose objA1.data stored something to be processed later.
Then I create an instance of class B, objB1, by passing objA1 into the instance constructor:
objB1 = ...
0
votes
0answers
13 views
Adding a method with a special method name to an existing object, which is also of a builtin type [duplicate]
I want to do the same as is described here, but I want to do it for the __call__ method, and I would like to do this for list objects:
>>> l = [1,2,3]
>>> l.__call__ = ...
1
vote
2answers
49 views
Python Class. key exists as self.key, but doesn't exists in self.__dict__
My class contain three big dicts, one appear in self.__dict__ other two not.
Example:
class MyClass:
big_matrix = {}
side_array = {}
map_of_data = {}
def __init__( self ):
# etc...
...
0
votes
2answers
48 views
How to save code doing it with OOP?
I'm writing a program to extract some data from txt files with regular expressions.
I'm new in OOP and want to save reiterative code. I want to retrieve about 15 data in each txt file, so I wrote a ...
2
votes
3answers
60 views
how to load variables from inherited class function in python
i want to ask about how to load variables from inherited class function in python
this is example of the first class
class Shape:
def __init__(self,x,y):
self.x = x
...
0
votes
1answer
36 views
Python change object reference from object itself
Consider the following code:
class A:
def hi(self):
print 'A'
def change(self):
self = B()
class B(A):
def hi(self):
print 'B'
and
test = A()
test.hi() -> ...
0
votes
2answers
63 views
Dynamically call overridden function
I iterate through a list and want to call a function on each item, but this function should be replaceable.
For example I have the following script:
class Parent(object):
def a(self, text):
...
0
votes
0answers
41 views
How and Why do I use Super() in Python [duplicate]
I've been teaching myself python for a few months now and I have just recently stumbled upon the super() method. I want to use it with the init() method when one class is inheriting from another ...
1
vote
1answer
71 views
Fast indexing / value setting in a list of objects
I'm writing a python package to decompose a speech signal into its spectral harmonics, so that each one of them could be represented as a complex exponential. A (very) simplified description of the ...
1
vote
2answers
29 views
how to return None if constructor arguments invalid
Currently I have something like
if name and password:
user = User(name, password)
...do stuff
I'd like to refactor it to something:
user = User(name, password)
if user:
...do stuff
...
0
votes
2answers
31 views
Python OOP how to return an object
I'm try to learn OOP in Python. The code below gives you a better idea of what I'm doing. I want to return an object what allows me to call other methods on that data. Is this the right way?
content ...