Very weird behavior that's driving me crazy

P

Pupeno

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>

ok, then

Manager.getStatus(self=<Sensor.Manager object at 0xb77cde8c>) =>
{"a": "b", "c": "d"}
None

None is the return of SensorSingleton.getStatus(), 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 ?
- What happened with all the output of SensorSingleton.getStatus() ? there's
no trace of it (but there's output of the method it calls).
- 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.

Thank you.
 
M

Marc 'BlackJack' Rintsch

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

How have you tested this? Is there any chance it was an interactive
session and you forgot to reload the module after making changes to it?

Ciao,
Marc 'BlackJack' Rintsch
 
S

Simon Forman

Pupeno said:
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>

ok, then

Manager.getStatus(self=<Sensor.Manager object at 0xb77cde8c>) =>
{"a": "b", "c": "d"}
None

None is the return of SensorSingleton.getStatus(), 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 ?
- What happened with all the output of SensorSingleton.getStatus() ? there's
no trace of it (but there's output of the method it calls).
- 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.

Thank you.

The code you posted doesn't match the output you posted. Try coding
the smallest version of what you're trying to do and post its output.

Peace,
~Simon
 
P

Pupeno

Nevermind, it was fixed. Thanks.
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>

ok, then

Manager.getStatus(self=<Sensor.Manager object at 0xb77cde8c>) =>
{"a": "b", "c": "d"}
None

None is the return of SensorSingleton.getStatus(), 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 ?
- What happened with all the output of SensorSingleton.getStatus() ?
there's no trace of it (but there's output of the method it calls).
- 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.

Thank you.
 
J

John Machin

Pupeno said:
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<---
 
B

Bruno Desthuilliers

Pupeno a écrit :
(snip)
and then I have another module called SensorSingleton that emulates the
hard-to-code-on-python singleton (snip)

What do you mean "hard to code on python singleton" ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top