在模块参数之间传递用户定义的问题

时间:2021-05-04 12:56:05

I am getting some odd behaviour passing an instance of Observable around. It seems to degenerate into a list and I cannot work out why.

我正在通过一个Observable实例获得一些奇怪的行为。它似乎退化成一个列表,我无法弄清楚为什么。

patterns.py

class Observable():

    def __init__ (self):
        self.observers = []

    def addObserver(self, observer):
        self.observers.append(observer)

    def notifyObservers(self, event):
        for observer in self.observers:
            observer.observeEvent(event)

    def __str__(self):
        print self.observers

views.py

from patterns import Observable

def loadData ():
    '''  helper function
    '''

    print "l1"
    avoidTollObservable = Observable ()

       dataLocation = os.path.join(os.path.dirname(__file__), 'DATA', "links.dat").replace('\\','/')

    print "reading edges"
    edgesDict = routing._readNetworkDataFile (dataLocation, avoidTollObservable)

routing.py

from patterns import Observable

def _readNetworkDataFile ( absFilePath , avoidTollObservable):

    print "debug:" 
    print avoidTollObservable
    print "debug end"

The program produces:

>
> debug:
> []
> [01/May/2013 08:40:49] "GET /route/-110884.64901689/6782501.6151699/-250884.64901689/7299908.890599588/?_=1367415648664 HTTP/1.1" 500 11571

The last line is in mauve (indicating that there is an error,I think)

最后一行是紫红色(表示有错误,我认为)

1 个解决方案

#1


1  

When doing this:

这样做时:

print avoidTollObservable

What you are basically doing is calling the Observable objects __str__() method

你基本上做的是调用Observable对象__str __()方法

def __str__(self):
    print self.observers # In your code this is declared as a list

The self.observers is an empty list (its how its initialised) so your output is going to be [], if you were to add a value add an item to self.observers before passing it to routing._readNetworkDataFile then the value of print avoidTollObservable would not be an empty list.

self.observers是一个空列表(它是如何初始化的)所以你的输出将是[],如果你要添加一个值将一个项添加到self.observers然后再传递给routing._readNetworkDataFile然后打印的值avoidTollObservable不是空列表。

If you were to change __str__() to:

如果您要将__str __()更改为:

def __str__(self):
    print 'Object ID' + self

Then the output would be the id of the object and not the value of the self.observers attribute.

然后输出将是对象的id而不是self.observers属性的值。

#1


1  

When doing this:

这样做时:

print avoidTollObservable

What you are basically doing is calling the Observable objects __str__() method

你基本上做的是调用Observable对象__str __()方法

def __str__(self):
    print self.observers # In your code this is declared as a list

The self.observers is an empty list (its how its initialised) so your output is going to be [], if you were to add a value add an item to self.observers before passing it to routing._readNetworkDataFile then the value of print avoidTollObservable would not be an empty list.

self.observers是一个空列表(它是如何初始化的)所以你的输出将是[],如果你要添加一个值将一个项添加到self.observers然后再传递给routing._readNetworkDataFile然后打印的值avoidTollObservable不是空列表。

If you were to change __str__() to:

如果您要将__str __()更改为:

def __str__(self):
    print 'Object ID' + self

Then the output would be the id of the object and not the value of the self.observers attribute.

然后输出将是对象的id而不是self.observers属性的值。