How to subclass a synchronized Queue with extra parameters










0















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).










share|improve this question


























    0















    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).










    share|improve this question
























      0












      0








      0








      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).










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 22:21









      Virgil GheorghiuVirgil Gheorghiu

      13619




      13619






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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






          share|improve this answer

























          • 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










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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






          share|improve this answer

























          • 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















          1














          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






          share|improve this answer

























          • 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













          1












          1








          1







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)