0

I have the following class:

class MessageContext:

    def __init__(self, raw_packet, packet_header, message_header, message_index):
        self.raw_packet = raw_packet
        self.pkthdr = packet_header
        self.msghdr = message_header
        self.msgidx = message_index
        self.msg_seqno = packet_header.seqno + message_index

And a function that creates objects using the above class:

def parsers(data): 
    ... 
    context = MessageContext(None, PacketAdapter(), msghdr, 0)
    self.on_message(rawmsg, context)

I am trying to recreate context, and when i set a breakpoint just after it and print context, I get:

<exchanges.protocols.blahblah.MessageContext object at 0x7337211520>

I have left out quite a bit of code as it is very long, but if any more information is needed I am happy to provide of course.

Here is what I get when I print the arguments of MessageContext:

print(PacketAdapter()) -> <exchanges.blahblah.PacketAdapter object at 0x7f60929e1820>


Following the comments below, the PacketAdapter() class looks like this:

class PacketAdapter:

    def __init__(self):
        self.seqno = 0  
Patrick_Chong
  • 434
  • 2
  • 12
  • What you are seeing is objects which do not have a defined representation and fallback to the default. But anyway, what is `self` in `parsers` ? and what state is kept in `PacketAdapter` ? Please [edit] your question and make sure to include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – ljmc Jan 21 '23 at 15:28
  • I can include `PacketAdapter` but it is the same as above. And I can include `self` of course. I am not sure in this instance what an 'example' would consist of! But let me add these to the question. – Patrick_Chong Jan 21 '23 at 15:51
  • never mind, I think it's too difficult to reproduce, `self` is a dataset of a class, the class which the function `parser` above belongs to. – Patrick_Chong Jan 21 '23 at 15:55
  • This is close to what I am after: https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print – Patrick_Chong Jan 21 '23 at 15:58
  • 1
    Why is it not *exactly* what you are after? – mkrieger1 Jan 21 '23 at 15:59
  • I just want to have the string/int/whatever representation of the class variable, but I can't seem to print it. – Patrick_Chong Jan 21 '23 at 15:59
  • @mkrieger1 it is not what I am after because in my unit test, lets say I want to call on the function `self.on_message(rawmsg, context)` from my question above, I need to pass in a `context` argument into the function right? But when I `print(context)` to see what this context argument looks, I get: – Patrick_Chong Jan 21 '23 at 16:00
  • Then as the question you have linked shows, you have to implement the `__str__` or `__repr__` method of the `PacketAdapter` class. Or, what do you hope to get instead of ``? – mkrieger1 Jan 21 '23 at 16:02
  • Yes, but what do I put inside the `__str__` or `__repr__`? Let me add what the `PacketAfapter` class looks like at the moment. I really appreciate the help! – Patrick_Chong Jan 21 '23 at 16:03
  • You put whatever you want to get out. – mkrieger1 Jan 21 '23 at 16:04
  • OK, but I want to see what is being passed in at the moment. I am debugging it. The issue is that I didn't write this code, I am simply running a long chain of functions, one of which called on the `PacketAdapter` class above. And I need to replicate this in a unit test. – Patrick_Chong Jan 21 '23 at 16:05
  • Ohhh is `self.seqno` what we are getting? – Patrick_Chong Jan 21 '23 at 16:08

0 Answers0