Pupeno wrote:
> Hello,
> I am experiencing a weird behavior that is driving me crazy. I have module
> called Sensors containing, among other things:
>
> class Manager:
>     def getStatus(self):
>         print "getStatus(self=%s)" % self
>         return {"a": "b", "c": "d"}
>
> and then I have another module called SensorSingleton that emulates the
> hard-to-code-on-python singleton in this way:
>
> manager = Manager()
> print "manager=%s" % manager
> def getStatus():
>     print "getStatus()"
>     return manager.getStatus()
>
> and then in some other module, I import SensorSingleton and I do, among
> other things:
>
>     print SensorSingleton.getStatus()
>
> and the output is more or less like this. First, the manager:
>
> manager: <Sensor.Manager object at 0xb7b9efec>
### Uh-oh. The code should print "manager=", not "manager:"
>
> ok, then
>
> Manager.getStatus(self=<Sensor.Manager object at 0xb77cde8c>) =>
### Uh-oh. The code should print "getStatus(self=blahblahblah" i.e.
without "Manager." in the front.

> {"a": "b", "c": "d"}
> None
>
> None is the return of SensorSingleton.getStatus(),
### What evidence do you have for that statement?

 now, my questions are:
>
> - Shouldn't the manager be the same in the first print and the second, that
> is, the id is different, shouldn't it be the same ?
### Yes.

> - What happened with all the output of SensorSingleton.getStatus() ? there's
> no trace of it (but there's output of the method it calls).

### All what output? The only expected output is "getStatus()"

> - Why doesn't the SensorSingleton.getStatus() return the return value of
> manager.getStatus(), it's a very straight forward call to it and return it.
>

If you want help, forget the "more or less" caper -- it's useless. It's
obvious from the above that the output that you describe can *not* have
come from the code fragments that you showed.Cut your 3 modules down to
the bare minimum that still shows the bug. Include copies of those plus
the exact (copy/paste) results of running the test at the OS
command-line (I.e. not in an IDE). Do make sure that none of your
modules are being shadowed or are outdated -- have all 3 in your
current working directory, and delete all .pyc files before running the
test.

What version of Python are you running? 2.3? Which platform?

Below's what I tried -- it's essentially the same as your code, with a
bit more output and with lower-case module names and I didn't bother
with a 2-line calling module.  It works as expected.

HTH,
John

C:\junk>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
|>>> import singleton
[singleton] manager=<sensors.Manager instance at 0x00AF43A0>
|>>> singleton.getStatus()
[singleton] entered getStatus()
[sensors]getStatus(self <sensors.Manager instance at 0x00AF43A0>)
[singleton] stuff = {'a': 'b', 'c': 'd'}
{'a': 'b', 'c': 'd'}
|>>> ^Z

C:\junk>type sensors.py
class Manager:
    def getStatus(self):
        print "[sensors]getStatus(self %s)" % self
        return {"a": "b", "c": "d"}

C:\junk>type singleton.py
from sensors import Manager
manager = Manager()
print "[singleton] manager=%s" % manager
def getStatus():
    print "[singleton] entered getStatus()"
    stuff = manager.getStatus()
    print "[singleton] stuff =", stuff
    return stuff
8<---

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to