Python对象共享信息

时间:2017-12-15 12:00:31

标签: python

我有一个解析url和loadsdata的工作流程。

流程通过URLProcessor控制。

在当前设置中,我将url变量传递到所有类,只是为了显示进程的开始和完成过程。我觉得它是多余的信息。 有没有办法在所有对象之间共享url变量,而不是明确地传递它。

class ParseURL(object):
    def __init__(self, url):
        self.url = url

    def parseoperation(self):
        print 'start operation for url {0}'.format(self.url)
        pass
        print 'end operation for url {0}'.format(self.url)
        return None

class LoadData(object):
    def __init__(self, url, data=None):
        self.url = url
        self.data = data

    def load_operation(self):
        print 'start operation for url {0}'.format(self.url)
        pass
        print 'end operation for url {0}'.format(self.url)


class URLProcessor(object):
    def __init__(self, url, parseurl, loaddata):
        self.url = url
        self.parseurl = parseurl
        self.loaddata = loaddata

    def run(self):
        print 'start operation for url {0}'.format(self.url)
        self.parseurl.parseoperation()
        self.loaddata.load_operation()
        print 'end operation for url {0}'.format(self.url)

url = 'http://test.com'
parseurl = ParseURL(url)
loaddata = LoadData(url)

urlprocessor = URLProcessor(url, parseurl, loaddata)
urlprocessor.run()

1 个答案:

答案 0 :(得分:2)

这里的简单解决方案是将class URLParser(object): def parse_operation(self, url): print 'start operation for url {0}'.format(url) print 'end operation for url {0}'.format(url) return None class DataLoader(object): def __init__(self, data=None): self.data = data def load_operation(self, url): print 'start operation for url {0}'.format(url) print 'end operation for url {0}'.format(url) class URLProcessor(object): def __init__(self, url, urlparser, dataloader): self.url = url self.urlparser = urlparser self.dataloader = dataloader def run(self): print 'start operation for url {0}'.format(self.url) self.urlparser.parse_operation(self.url) self.dataloader.load_operation(self.url) print 'end operation for url {0}'.format(self.url) url = 'http://test.com' urlparser = URLParser() dataloader = DataLoader() urlprocessor = URLProcessor(url, urlparser, dataloader) urlprocessor.run() 传递给' xxx_operation'方法调用而不是在instanciation传递它们:

def parse_url(url):
    print 'start operation for url {0}'.format(url)
    print 'end operation for url {0}'.format(url)

def load_data(url, data=None):
    print 'start operation for url {0}'.format(url)
    print 'end operation for url {0}'.format(url)

def process_url(url, parse_url, load_data, data=None):
    print 'start operation for url {0}'.format(url)
    parse_url(url)
    load_data(url, data)
    print 'end operation for url {0}'.format(url)

url = 'http://test.com'
process_url(url, parse_url, load_data)

正如托马斯在评论中提到的那样,并且只根据这个有希望删节的片段进行评判,你似乎真的不需要上课:

__call__(self, ...)

请记住,在Python中,所有内容(除了语句除外)都是一个对象,包括函数,类和模块。如果你发现自己用一种方法编写一个类,那么你很可能需要一个函数。还要记住,你可以定义你自己的callables(在你的类上定义load_data方法),所以一个期望回调的类或方法不仅可以传递函数,而且可以传递给任何可调用的 - 也就是说,如果你发现了你的d = d为了便于阅读或出于任何其他原因而将process_url()作为类进行实施,您仍然可以在不更改class MyDataLoader(object): def __init__(self, db): self.db def _do_something(self, args): pass def _do_something_else(self, args): pass def __call__(self, url, data): print 'start operation for url {0}'.format(url) self._do_something(url) self._do_something_else(data) print 'end operation for url {0}'.format(url) url = 'http://test.com' db = MyDB.connect(connectioninfos) load_data = MyDataLoader(db) process_url(url, parse_url, load_data) db.close() 的情况下执行此操作:

var schema = new mongoose.Schema({
 subtotal: {type: Number, required: true, default: 0},
 owner: { type : Number, required : true, index: true},
 products: [{type: mongoose.Schema.ObjectId, ref: 'Product'}],
}, {
 timestamps: true
});


schema.path('subtotal').set(function(p){
  return p * 100;
});

schema.path('subtotal').get(function(p){
  return parseFloat((p/100).toFixed(2));
});

schema.set('toJSON', {getters: true, setters:true});
schema.set('toObject', {getters: true, setters:true});