Tagged Questions
1
vote
2answers
37 views
Overriding methods without falling into infinite recursion
I want to override a method of the class dict in Python, a simple one: update. Let's say I want to create a MyDict class which is identical to standard dict except that it can be updated with dicts ...
-3
votes
1answer
40 views
Override __str__ of parent class
I have a problem and I can't seem to find a solution. I have 3 classes A, B and C, with A being the superclass.
class A(object):
def __str__(self):
self._general_representation(str)
...
2
votes
2answers
64 views
python built-in functions vs magic functions and overriding [duplicate]
Possible Duplicate:
Intercept operator lookup on metaclass
How can I intercept calls to python’s “magic” methods in new style classes?
Consider the following code:
class ClassA(object):
...
1
vote
1answer
63 views
django manytomanyfield .add() method
Suppose i have :
class Album(models.Model):
photos = models.ManyToManyField('myapp.Photo')
photo_count = models.IntegerField(default = 0)
class Photo(models.Model):
photo = ...
3
votes
1answer
71 views
How to override __setitem__ for a function's globals dict?
I want to be able to intercept variable assignments within a function and execute custom code. I have tried creating a custom dict as follows:
class userdict(dict):
def __setitem__(self, key, ...
2
votes
2answers
75 views
When to call a super-class' method?
I'm working on extending a project maintained by a group of developers. My code involves overriding of methods in the classes from existing code. One of the points of discomfort to me is that I don't ...
1
vote
1answer
63 views
Django Model: How to use mixin class to override django model for function likes save
I want to validate value before every model save. So, I must override the save function.
The code is nearly just the same on, and I want to write it in a mixin class. But failed for
I don't know how ...
3
votes
2answers
164 views
subclass method overwriting in python
I write a test code of python like:
class Parent(object):
@classmethod
def dispatch(klass):
print 'klass = %s' % klass
return klass().__dispatch()
def __dispatch(self):
...
4
votes
2answers
104 views
Does an equivalent of override exist for nested functions?
If I have this function, what should I do to replace the inner function with my own custom version?
def foo():
def bar():
# I want to change this
pass
# here starts a long ...
2
votes
2answers
169 views
Method overriding by monkey patching
I'm trying to figure out why the following example won't work.
class BaseClass(object):
def __init__(self):
self.count = 1
def __iter__(self):
return self
def ...
2
votes
3answers
127 views
Define method aliases in Python?
I have a vector class and I defined the __mul__ method to multiply a vector by a number.
Here is the __mul__ method :
def __mul__(self, other):
x = self.x * other
y = self.y * other
new ...
0
votes
2answers
88 views
How to return a value computed by overriding method from a subclass to it's base class method in python
I have a base class like this:
class base(object):
name = ""
def __init__(self,name):
self.user =user
def context(self,value):
return {}
Instead of this i created an ...
1
vote
2answers
160 views
How to use SQLAlchemy reflection with Sybase? [answer: turns out it's not supported!]
I'm trying to learn more about the .egg concept and overriding methods in Python. Here's the error message I'm receiving:
Traceback (most recent call last):
File ...
1
vote
1answer
164 views
python override built-in classes, in particular, dictionary class
I've never really like the way the dictionary class is converted into a string so I wrote a subclass which overrides the repr method (this method uses tabs to representing the level of nesting in the ...
2
votes
1answer
81 views
Overriding operators within functions
I want to define operators for a class instance within a class function, like so:
class MyClass(object):
@property
def _arithmetic_threshold(self):
return self.threshold # this will ...