How to subclass a synchronized Queue with extra parameters
I am trying to subclass Queue and the following code throws an exception:
from Queue import Queue
class MyQueue(Queue, object):
def __init__(self, my_stuff = None):
super(MyQueue, self).__init__()
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
And I'm calling it from __main__
like so:
a_queue = MyQueue(my_stuff = "some stuff")
print a_queue
I am getting, as expected:
$ ./my_queue.py
some stuff
But I only get that if I also inherit from object
(per new-style classes). Otherwise I get:
Traceback (most recent call last):
File "./my_queue", line 13, in <module>
a_queue = MyQueue(my_stuff = "some stuff")
File "./my_queue", line 6, in __init__
super(MyQueue, self).__init__()
TypeError: must be type, not classobj
Why? It apparently doesn't work for Queue as easily as it does for e.g. mp.Process (see this question and also this question).
python-2.7
add a comment |
I am trying to subclass Queue and the following code throws an exception:
from Queue import Queue
class MyQueue(Queue, object):
def __init__(self, my_stuff = None):
super(MyQueue, self).__init__()
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
And I'm calling it from __main__
like so:
a_queue = MyQueue(my_stuff = "some stuff")
print a_queue
I am getting, as expected:
$ ./my_queue.py
some stuff
But I only get that if I also inherit from object
(per new-style classes). Otherwise I get:
Traceback (most recent call last):
File "./my_queue", line 13, in <module>
a_queue = MyQueue(my_stuff = "some stuff")
File "./my_queue", line 6, in __init__
super(MyQueue, self).__init__()
TypeError: must be type, not classobj
Why? It apparently doesn't work for Queue as easily as it does for e.g. mp.Process (see this question and also this question).
python-2.7
add a comment |
I am trying to subclass Queue and the following code throws an exception:
from Queue import Queue
class MyQueue(Queue, object):
def __init__(self, my_stuff = None):
super(MyQueue, self).__init__()
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
And I'm calling it from __main__
like so:
a_queue = MyQueue(my_stuff = "some stuff")
print a_queue
I am getting, as expected:
$ ./my_queue.py
some stuff
But I only get that if I also inherit from object
(per new-style classes). Otherwise I get:
Traceback (most recent call last):
File "./my_queue", line 13, in <module>
a_queue = MyQueue(my_stuff = "some stuff")
File "./my_queue", line 6, in __init__
super(MyQueue, self).__init__()
TypeError: must be type, not classobj
Why? It apparently doesn't work for Queue as easily as it does for e.g. mp.Process (see this question and also this question).
python-2.7
I am trying to subclass Queue and the following code throws an exception:
from Queue import Queue
class MyQueue(Queue, object):
def __init__(self, my_stuff = None):
super(MyQueue, self).__init__()
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
And I'm calling it from __main__
like so:
a_queue = MyQueue(my_stuff = "some stuff")
print a_queue
I am getting, as expected:
$ ./my_queue.py
some stuff
But I only get that if I also inherit from object
(per new-style classes). Otherwise I get:
Traceback (most recent call last):
File "./my_queue", line 13, in <module>
a_queue = MyQueue(my_stuff = "some stuff")
File "./my_queue", line 6, in __init__
super(MyQueue, self).__init__()
TypeError: must be type, not classobj
Why? It apparently doesn't work for Queue as easily as it does for e.g. mp.Process (see this question and also this question).
python-2.7
python-2.7
asked Nov 12 '18 at 22:21
Virgil GheorghiuVirgil Gheorghiu
13619
13619
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The problem is that the Queue class does not support the super feature since it is an old style class.
This way inheriting from object will solve your problem (make it a new style class).
But if you want to avoid it just call init directly
change it to:
from Queue import Queue
class MyQueue(Queue):
def __init__(self, my_stuff = None):
Queue.__init__(self)
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
while old classes in python cannot do this New style classes may use super(Foo, self)
where Foo is the class and self is the instance.
super(type[, object-or-type])
Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class. The search order is same as that
used by getattr() except that the type itself is skipped.
The new style class is usually a better choice
Thanks, Omer. Please update the URLs in your answer to Python 2.7 and not Python 3.x, since I tagged the question with this version of Python.
– Virgil Gheorghiu
Nov 13 '18 at 17:26
thanks, done. if this answer solved your problem can you accept it?
– Omer Ben Haim
Nov 14 '18 at 10:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270940%2fhow-to-subclass-a-synchronized-queue-with-extra-parameters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that the Queue class does not support the super feature since it is an old style class.
This way inheriting from object will solve your problem (make it a new style class).
But if you want to avoid it just call init directly
change it to:
from Queue import Queue
class MyQueue(Queue):
def __init__(self, my_stuff = None):
Queue.__init__(self)
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
while old classes in python cannot do this New style classes may use super(Foo, self)
where Foo is the class and self is the instance.
super(type[, object-or-type])
Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class. The search order is same as that
used by getattr() except that the type itself is skipped.
The new style class is usually a better choice
Thanks, Omer. Please update the URLs in your answer to Python 2.7 and not Python 3.x, since I tagged the question with this version of Python.
– Virgil Gheorghiu
Nov 13 '18 at 17:26
thanks, done. if this answer solved your problem can you accept it?
– Omer Ben Haim
Nov 14 '18 at 10:13
add a comment |
The problem is that the Queue class does not support the super feature since it is an old style class.
This way inheriting from object will solve your problem (make it a new style class).
But if you want to avoid it just call init directly
change it to:
from Queue import Queue
class MyQueue(Queue):
def __init__(self, my_stuff = None):
Queue.__init__(self)
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
while old classes in python cannot do this New style classes may use super(Foo, self)
where Foo is the class and self is the instance.
super(type[, object-or-type])
Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class. The search order is same as that
used by getattr() except that the type itself is skipped.
The new style class is usually a better choice
Thanks, Omer. Please update the URLs in your answer to Python 2.7 and not Python 3.x, since I tagged the question with this version of Python.
– Virgil Gheorghiu
Nov 13 '18 at 17:26
thanks, done. if this answer solved your problem can you accept it?
– Omer Ben Haim
Nov 14 '18 at 10:13
add a comment |
The problem is that the Queue class does not support the super feature since it is an old style class.
This way inheriting from object will solve your problem (make it a new style class).
But if you want to avoid it just call init directly
change it to:
from Queue import Queue
class MyQueue(Queue):
def __init__(self, my_stuff = None):
Queue.__init__(self)
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
while old classes in python cannot do this New style classes may use super(Foo, self)
where Foo is the class and self is the instance.
super(type[, object-or-type])
Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class. The search order is same as that
used by getattr() except that the type itself is skipped.
The new style class is usually a better choice
The problem is that the Queue class does not support the super feature since it is an old style class.
This way inheriting from object will solve your problem (make it a new style class).
But if you want to avoid it just call init directly
change it to:
from Queue import Queue
class MyQueue(Queue):
def __init__(self, my_stuff = None):
Queue.__init__(self)
self.my_stuff = my_stuff
def my_function(self):
return self.my_stuff
while old classes in python cannot do this New style classes may use super(Foo, self)
where Foo is the class and self is the instance.
super(type[, object-or-type])
Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class. The search order is same as that
used by getattr() except that the type itself is skipped.
The new style class is usually a better choice
edited Nov 14 '18 at 10:11
answered Nov 13 '18 at 9:54
Omer Ben HaimOmer Ben Haim
1788
1788
Thanks, Omer. Please update the URLs in your answer to Python 2.7 and not Python 3.x, since I tagged the question with this version of Python.
– Virgil Gheorghiu
Nov 13 '18 at 17:26
thanks, done. if this answer solved your problem can you accept it?
– Omer Ben Haim
Nov 14 '18 at 10:13
add a comment |
Thanks, Omer. Please update the URLs in your answer to Python 2.7 and not Python 3.x, since I tagged the question with this version of Python.
– Virgil Gheorghiu
Nov 13 '18 at 17:26
thanks, done. if this answer solved your problem can you accept it?
– Omer Ben Haim
Nov 14 '18 at 10:13
Thanks, Omer. Please update the URLs in your answer to Python 2.7 and not Python 3.x, since I tagged the question with this version of Python.
– Virgil Gheorghiu
Nov 13 '18 at 17:26
Thanks, Omer. Please update the URLs in your answer to Python 2.7 and not Python 3.x, since I tagged the question with this version of Python.
– Virgil Gheorghiu
Nov 13 '18 at 17:26
thanks, done. if this answer solved your problem can you accept it?
– Omer Ben Haim
Nov 14 '18 at 10:13
thanks, done. if this answer solved your problem can you accept it?
– Omer Ben Haim
Nov 14 '18 at 10:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270940%2fhow-to-subclass-a-synchronized-queue-with-extra-parameters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown