2010-09-13 27 views
5

Tôi có đoạn mã sau đây mà tôi cố gắng ghi đè lên một phương pháp:Python lớp kỳ quái vấn đề

import Queue 
class PriorityQueue(Queue.PriorityQueue): 
    def put(self, item): 
     super(PriorityQueue, self).put((item.priority, item)) 

Tuy nhiên, khi tôi chạy nó tôi nhận được TypeError ngoại lệ:

super() argument 1 must be type, not classobj 

là gì vấn đề?

Trả lời

7

Queue.PriorityQueue không phải là lớp kiểu mới và superonly works with new-style classes. Bạn phải sử dụng

import Queue 
class PriorityQueue(Queue.PriorityQueue): 
    def put(self, item): 
     Queue.PriorityQueue.put(self,(item.priority, item)) 

để thay thế.

+0

Hoàn hảo. Cảm ơn rất nhiều! – yassin

+2

thực sự, bạn cũng sẽ cần phải vượt qua 'tự' một cách rõ ràng: Queue.PriorityQueue.put (self, (item.priority, item)) –

+0

@Ivo: Có; Cảm ơn vì sự đúng đắn của bạn! – unutbu