need some basic help

Steven D'Aprano steve at pearwood.info
Thu Dec 24 06:58:47 EST 2015


On Thu, 24 Dec 2015 12:53 pm, Qurrat ul Ainy wrote:

> Hello,
> 
> Can someone please explain this code below. I am new at Python .
> Thanks
> 
> 
> def receive_messages(self, msgs, time):
>       for msg in msgs:
>          msg.set_recv_time(time)
>       self.msgs_received.extend(msgs)


This is a method belonging to a class. We don't know what the class is,
because you haven't shown it, so let's just call it "MyClass".

The method takes two arguments (msgs and time) plus a special
argument, "self", which represents the instance where the method is called.
So at some point, you must say:

    instance = MyClass()

Do you understand object oriented programming? Have you programmed in any
other languages?

My *guess* is that the "msgs" argument is probably a list of messages:

    the_messages = [first_message, second_message, third_message]

and the "time" argument is possibly a number (time in seconds?):

    some_time = 125


Then you call the method:

    instance.receive_messages(the_messages, some_time)


When you call the receive_messages method, the following happens:

- each of the messages are taken in turn, one at a time, and a method on
that message is called:

    for msg in msgs:
        msg.set_recv_time(time)

    Translation:
    for each message first_message, second_message, ...
        call method "set_recv_time" with argument some_time

- then the original instance calls one of its own messages, probably to
store those messages:

    self.msgs_received.extend(msgs)




I can't be more detailed, as I don't know how much programming experience
you have, or what the class that this method came from does.


-- 
Steven




More information about the Python-list mailing list