0
print("Device discovered: %s" % remote)
m.append(remote)
print(m) 

I am getting the following result. How Can I get same value is the list names m

Device discovered: 0013A20041481642 - REMOTE
[<digi.xbee.devices.RemoteZigBeeDevice object at 0x7f42c312ceb8>]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
arun soman
  • 21
  • 8
  • You have to convert it to a string literal before you print it. – Vasilis G. Nov 13 '17 at 20:55
  • Not sure what you expected or what you think is unexpected. Is the problem that you are surprised by the difference between `0013A20041481642` (a number used by the `RemoteZigBeeDevice` for whatever zigbee-specific purpose) and `0x7f42c312ceb8` (an ID assigned by the Python interpreter itself to the `RemoteZigBeeDevice` instance, just like it will assign an ID to any object...) – jez Nov 13 '17 at 20:57

2 Answers2

2

List contents use the repr() result of the objects contained, which in turn uses the __repr__ method on an object.

print() converts objects with str(), which will use a __str__ method, if present, __repr__ otherwise.

You remote object (a digi.xbee.devices.RemoteZigBeeDevice instance) only has a __str__ method, and no __repr__ method. Implement the latter to customise how your object is shown in a list.

Alternatively, print all individual values in the list with:

print(*m)

These are then space separated, use sep='...' to specify a different separator.

Or you could convert all the objects in the list to a string before printing:

print([str(obj) for obj in m])

and print the list of strings.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Did you try m.append(str(remote))? It is possible there is a __str__ function implemented in remote object. Such magic function will handle object to string conversion when it's necessary. When you format the object it is necessary to convert to str but when you append it to a list it's not.

Haochen Wu
  • 1,753
  • 1
  • 17
  • 24