bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gnulib-tool.py: Simplify data structures for dependencies.


From: Bruno Haible
Subject: Re: gnulib-tool.py: Simplify data structures for dependencies.
Date: Fri, 19 Apr 2024 23:37:49 +0200

Hi Collin,

> Patch 0001 does the same but uses a set instead of checking for
> membership in a list.

Thanks; applied.

> Also, am I missing something or are the sets/lists at
> self.dependers[str(module)] unused?

You're right. Maybe Dmitry wanted to have the full data structure
available somewhere; but since the 'conditionals' and 'unconditionals'
tables contain the same info in a different way, it's redundant.

I would leave it in, for possible future uses, since it does not consume
much memory. Just add a comment that it's currently unused, and is kept
for possible future use.

> Patch 0002 changes the str(module) inserted into lists in that section
> of code to directly use the GLModule object. For accessing the
> conditionals dictionaries we can use a tuple:
> 
>     (parent, module)
> 
> instead of a string like gnulib-tool.sh:
> 
>     '%s---%s' % (str(parent), str(module))

Good idea, to use the Python tuple type, rather than an ad-hoc hash table
key.

> Also the str(module) sort of makes things harder to read. It is mostly
> my fault that we have three ways to get a GLModule objects name:
> 
>     str(module)
>     module.name
>     module.getName()
> 
> which are all used in various places...

Indeed. I would not make use of
  str(module)
since it may be useful to have str(module) look like this:
#<GLModule name=foo conditionals=... unconditionals=... ...>

In pure OO, one would not make the 'name' public, since it's meant to
be a read-only property. It eliminates possible side effects on the
'name' property from the beginning.

But what is common in Python? Let's ask the engine:
--------------------------------------------------------------------------
Q: In Python, when I have a class whose instances are uniquely
   identified by a name, and the name of an instance cannot be
   changed after the instance has been constructed, which is the
   preferred way of accessing the name? Is it `str(inst)`, `inst.name`,
   `inst.get_name()`, or `inst.getName()` ?
A: The preferred way of accessing the name of an instance in Python
   depends on the design of your class and your specific requirements.
   However, considering your criteria where the name cannot be changed
   after construction, you might want to use inst.name if the name is
   a property of the instance and is directly accessible.

   Here's a brief overview of each option:

   1. str(inst): This will call the __str__ method of your class. If
      you've implemented __str__ to return the name of the instance,
      this could work. However, using str(inst) to access the name might
      not be as clear in terms of code readability.
   2. inst.name: If the name is stored directly as an attribute of the
      instance and is meant to be accessed directly, this is a clear
      and concise way to access it.
   3. inst.get_name(): This method would be appropriate if you need to
      perform some processing or validation before returning the name.
      It adds a layer of abstraction, which can be useful in some cases,
      but it might be unnecessary overhead if you're simply returning
      the name.
   4. inst.getName(): This is similar to inst.get_name() but follows the
      convention of using CamelCase for method names, which is common in
      some coding styles. However, in Python, the convention is typically
      to use lowercase letters and underscores for method names (get_name()).

   Ultimately, the choice depends on factors such as readability,
   consistency with the rest of your codebase, and whether you need any
   additional logic when accessing the name.
--------------------------------------------------------------------------

I will let you evaluate this. I have nothing to add :-)

Bruno






reply via email to

[Prev in Thread] Current Thread [Next in Thread]