0

For example, if I want to send a message:

message_sender = MessageSender()

sent_message = message_sender.Send("test")

Is it fine if i make that Send method return some data, for example the time at which the message was sent, and bind it to a variable so Ì can use that data later, or is this just confusing design?

If this is not acceptable, how should I do it? To clarify, I send a message using that code, and bind some data regarding that message to the sent_message variable.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Parsee
  • 71
  • 6
  • 5
    You're saving the return value to a variable. What's wrong with that? – Maroun Dec 14 '15 at 07:57
  • Where are you binding a method to a variable here? You are using the **return value** of a method call. – Martijn Pieters Dec 14 '15 at 08:00
  • @MarounMaroun Saving return value to a variable could confuse someone reading the code, they might think that I'm just saving some data to a variable, but in fact I'm also sending some data elsewhere. Normally I have only seen people use methods without saving their return value. – Parsee Dec 14 '15 at 08:01
  • @Parsee You're indeed sending data somewhere, but that method returns a value. It might indicate success or failure of the operation, and you're free to use that value as you wish. – Maroun Dec 14 '15 at 08:03
  • I wouldn't expect a `Send` method to return the message. `sent_message= "test"; send(sent_message)` seems more natural to me. I am less reluctant for other state information, in particular diagnostics. –  Dec 14 '15 at 08:03
  • You're usage of the word **bind** is different than how it is normally used when referring to python objects. **Bound** and **Unbound** have very specific meanings within python -- see this [question](http://stackoverflow.com/questions/114214/class-method-differences-in-python-bound-unbound-and-static) It would be more clear to say that you're just saving the return value to a variable. – Brendan Abel Dec 14 '15 at 08:04
  • 1
    @BrendanAbel: nope, assignment to a variable is [also called binding](https://docs.python.org/2/reference/executionmodel.html#naming-and-binding). – Martijn Pieters Dec 14 '15 at 08:06
  • @YvesDaoust: The method isn't returning its message argument, it's returning the timestamp of when the message was sent. FWIW, I agree that this is perfectly acceptable behaviour. – PM 2Ring Dec 14 '15 at 08:07
  • @PM2Ring: then the variable name `sent_message` is misleading. –  Dec 14 '15 at 08:09
  • @MartijnPieters I've only heard it used in that context when describing scope (ie. bound within a certain context), or if something was being monkey-patched. Generally, when talking about a variable, it's *usually* described as *declaring* or *defining* or *assigning* a variable. – Brendan Abel Dec 14 '15 at 08:15
  • @YvesDaoust: I suppose so. But naming things is hard. :) – PM 2Ring Dec 14 '15 at 08:16
  • @PM2Ring: I would call a timestamp a timestamp, not a message. –  Dec 14 '15 at 08:17
  • 1
    @BrendanAbel: not in Python it isn't. Python doesn't declare variables. Names exist because they have been bound to. See the first sentence of that documentation. The other context is when binding methods to instances. – Martijn Pieters Dec 14 '15 at 08:29

1 Answers1

1

There's absolutely nothing wrong with what you're doing. Python's own socket.send() method returns the number of bytes written, for example: https://docs.python.org/2/library/socket.html#socket.socket.send

John Zwinck
  • 239,568
  • 38
  • 324
  • 436