Python Forum
Which method name would you choose?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Which method name would you choose?
#1
I currently have this syntax
preamble, content = mediator.read()
Here, mediator is an instance of a Mediator class, which you can view as a smart pointer referencing a resource stored somewhere. The read() method accesses the resource and gets two things: a 'preamble' (instance of a Preamble class) and a 'content' which is a unicode string.

Problem: the method name read() doesn't seem very good to me because one usually expects that a read() method returns a string, but here it returns a tuple with two elements.

English is not my mother tongue, so what better word would you choose to name this method? The idea is to dereference the 'smart pointer' to get the pointed data.
« We can solve any problem by introducing an extra level of indirection »
Reply
#2
mediator.get_pointer_data()?
Reply
#3
May be a bit wordy but mediator.get_preamble_and_content() leaves nothing to the imagination.
Reply
#4
Use type hints

mediator.read_data() -> tuple[preamble, content]:
mediator.import_data() -> tuple[preamble, content]:

this way you can change / append later
Reply
#5
Thank you all for your input. I think get_preamble_and_content() is too specific. It gives the impression that a mediator is a container which it is not, I want to keep it clear that it is some kind of pointer. get_pointer_data() and read_data() are a little verbose, the 'data' could be avoided. I think I'll use the following
resource = mediator.get()
# resource has members
resource.preamble
resource.text
The .get() makes it clear that we are dereferencing something. A known example is
response = requests.get(url)
We can see a url as a pointer to a resource on the internet, and it suffices to indicate that we dereference this pointer. A benefit of returning a single object is that this object's content can change later more easily.

There is an example of a smart pointer in the standard library: weak references
>>> from weakref import ref
>>> class A: pass
... 
>>> a = A()
>>> r = ref(a)
>>> r
<weakref at 0x7fedeedb1350; to 'A' at 0x7fedeed7f490>
>>> r()
<__main__.A object at 0x7fedeed7f490>
Here the __call__() is used to dereference the pointer, but I think it is cryptic for the reader of the program. get() is better than call.

Another example is that of tkinter variables, which are just pointers dereferenced by the get() method.
string = stringvar.get()
Thanks again, if you have more ideas about this, please add them to this thread.
PyDan likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#6
(Disclaimer, my primary language is C++, so …)

To me, get with no arguments means return an instance, I would read it as.
mediator.get()
returns a mediator.
Exactly as you mention “just pointers dereferenced by the get() method”

But now, I read
resource = mediator.get()
and I get confused, wait is resource a mediator or a tuple containing preamble and content?

If resource is a mediator with properties, preamble and content, then .get is perfect
Reply
#7
(May-30-2024, 01:29 AM)PyDan Wrote: To me, get with no arguments means return an instance, I would read it as.
You're probably biased by your C++ culture, although cin.get() does not return an input stream in C++.

The problem is that we don't have a star operator to dereference pointers in Python. It is not possible to write
resource = *mediator
So the .get() method appears to be a simple way to mean 'dereference that pointer', unless you see a better alternative?

I would never interprete mediator.get() as 'get a Mediator'. Also in the Python culture, the example of the tkinter variables enlights the meaning of this 'get()'.
PyDan likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#8
IMHO, read is perfectly fine, especially in computer/programming context. Expecting read to return string is too narrow definition.
See e.g. https://www.google.com/search?&q=read+meaning See e.g. 3 or 6

Quote:...
inspect and record the figure indicated on (a measuring instrument).
"I've come to read the gas meter"

...

(of a computer) copy, transfer, or interpret (data).
"it attempts to read the disk without regard to its format"

enter or extract (data) in an electronic storage device.
"the commonest way of reading a file into the system"
(of a device) obtain data from (light or other input).
"the microchip gives a unique code when read by the scanner"
...

Disclaimer: I am not native English speaker
Gribouillis likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mac os choose file name howard687 1 1,965 Jan-05-2022, 06:54 AM
Last Post: Larz60+
  Loop with choose Irv1n 5 3,388 Sep-16-2021, 09:31 PM
Last Post: deanhystad
  Choose an element from multidimensional array quest_ 2 2,750 Nov-25-2020, 12:59 AM
Last Post: quest_
  Choose your own adventure game noahc2004 2 2,699 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Please help a newbie choose which programming language to learn. yeto 2 3,629 Feb-25-2019, 12:56 AM
Last Post: yeto
  User Input to Choose from Dictionary anelliaf 9 26,138 Mar-27-2018, 02:22 PM
Last Post: anelliaf
  Reasons to choose Python over C++? RandoomDude 62 47,186 May-03-2017, 05:29 PM
Last Post: micseydel
  Need a little more help in a Choose Your Own Adventure Program Goldberg291 13 18,930 Jan-31-2017, 08:33 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020