• Where I do ask for a new feature

    From Bongo Ferno@21:1/5 to All on Mon Oct 16 16:35:26 2023
    Where I can ask python developers for a new feature?

    This feature would allow us to create short aliases for long object paths, similar to the with statement. This would make code more readable and maintainable.

    For example, if we have a long object like "MyObject.stuff.longStuff.SubObject", we could create a short alias for it like this:


    aliasView my_object.stuff.long_stuff.sub_object as short_view
    #Now, we can operate with the nested object using the short alias:
    print(short_view.some_method())

    This is much more concise and readable than having to write out the full object path every time.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to [email protected] on Tue Oct 17 12:57:25 2023
    On Tue, 17 Oct 2023 at 12:55, Bongo Ferno via Python-list <[email protected]> wrote:

    Where I can ask python developers for a new feature?

    This feature would allow us to create short aliases for long object paths, similar to the with statement. This would make code more readable and maintainable.

    For example, if we have a long object like "MyObject.stuff.longStuff.SubObject", we could create a short alias for it like this:


    aliasView my_object.stuff.long_stuff.sub_object as short_view
    #Now, we can operate with the nested object using the short alias:
    print(short_view.some_method())

    This is much more concise and readable than having to write out the full object path every time.


    You can actually just do that with simple assignment!

    short_view = my_object.stuff.long_stuff.sub_object print(short_view.some_method())

    It'll work!

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bongo Ferno@21:1/5 to All on Thu Oct 19 18:32:34 2023
    You can actually just do that with simple assignment!

    short_view = my_object.stuff.long_stuff.sub_object print(short_view.some_method())

    but then have to delete the variable manually

    del short_view

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Thu Oct 19 22:26:20 2023
    Bongo,

    Variables in most programming languages either have to be removed manually
    or allowed to drift outside a boundary when they disappear for scoping
    reasons and perhaps are garbage collected at some point.

    There are many ways to make transient variables that disappear at some time
    and do we need yet another? Yes, you can create one of those ways but what
    is the big deal with deleting a variable when no longer used?

    Examples might be the "finally" clause or the "with" statement or just
    putting the variable in a nested scope.

    -----Original Message-----
    From: Python-list <python-list-bounces+avi.e.gross=[email protected]> On Behalf Of Bongo Ferno via Python-list
    Sent: Thursday, October 19, 2023 9:33 PM
    To: [email protected]
    Subject: Re: Where I do ask for a new feature


    You can actually just do that with simple assignment!

    short_view = my_object.stuff.long_stuff.sub_object print(short_view.some_method())

    but then have to delete the variable manually

    del short_view
    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bongo Ferno@21:1/5 to [email protected] on Thu Oct 19 20:16:37 2023
    On Thursday, October 19, 2023 at 11:26:52 PM UTC-3, [email protected] wrote:

    There are many ways to make transient variables that disappear at some time and do we need yet another? Yes, you can create one of those ways but what is the big deal with deleting a variable when no longer used?

    Assigning a variable to something can be anything else than a temporal alias.
    A with statement makes clear that the alias is an alias and is local, and it automatically clears the variable after the block code is used.

    Python clutters the variable space with vars that are needed only on certain places, and an alias doesn't has a scope.
    Convenient alias are short names, and short names are limited in quantity. If the space is cluttered with short alias, it opens risks for wrong utilization.

    Its like writing a "for i" in a list comprehension and having to worry if "i" was already used in another place..

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Bongo Ferno on Fri Oct 20 17:41:32 2023
    On 19Oct2023 20:16, Bongo Ferno <[email protected]> wrote:
    A with statement makes clear that the alias is an alias and is local,
    and it automatically clears the variable after the block code is used.

    No it doesn't:

    >>> with open('/dev/null') as f:
    ... print(f)
    ...
    <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
    >>> print(f)
    <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to All on Fri Oct 20 09:55:12 2023
    Op 20/10/2023 om 5:16 schreef Bongo Ferno via Python-list:
    On Thursday, October 19, 2023 at 11:26:52 PM UTC-3, [email protected] wrote:

    There are many ways to make transient variables that disappear at some time and do we need yet another? Yes, you can create one of those ways but what is the big deal with deleting a variable when no longer used?

    Assigning a variable to something can be anything else than a temporal alias. A with statement makes clear that the alias is an alias and is local, and it automatically clears the variable after the block code is used.

    Python clutters the variable space with vars that are needed only on certain places, and an alias doesn't has a scope.
    Convenient alias are short names, and short names are limited in quantity. If the space is cluttered with short alias, it opens risks for wrong utilization.

    Its like writing a "for i" in a list comprehension and having to worry if "i" was already used in another place..
    As long as functions are kept reasonably short, which is a good idea
    anyway, I don't really see any of that as a problem.

    --
    "Experience is that marvelous thing that enables you to recognize a
    mistake when you make it again."
    -- Franklin P. Jones

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Bongo Ferno on Fri Oct 20 11:58:26 2023
    Bongo Ferno <[email protected]> writes:
    A with statement makes clear that the alias is an alias and
    is local, and it automatically clears the variable after the
    block code is used.

    Python does not seem to have blocks in the way of C and
    many other languages.

    One could use a "del" to clarify the scope:

    tmp = x
    x = y
    y = tmp
    del tmp

    . But I believe that the mainstream sees this as bad style.

    What comes most close to a block in Python, is a function definition:

    def f( ... ):
    tmp = ...

    . This expresses that the scope of "tmp" is that function definition.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Bongo Ferno via Python-list on Fri Oct 20 08:32:02 2023
    On 10/19/2023 11:16 PM, Bongo Ferno via Python-list wrote:
    On Thursday, October 19, 2023 at 11:26:52 PM UTC-3, [email protected] wrote:

    There are many ways to make transient variables that disappear at some time >> and do we need yet another? Yes, you can create one of those ways but what >> is the big deal with deleting a variable when no longer used?

    Assigning a variable to something can be anything else than a temporal alias. A with statement makes clear that the alias is an alias and is local, and it automatically clears the variable after the block code is used.

    Python clutters the variable space with vars that are needed only on certain places, and an alias doesn't has a scope.
    Convenient alias are short names, and short names are limited in quantity. If the space is cluttered with short alias, it opens risks for wrong utilization.

    Its like writing a "for i" in a list comprehension and having to worry if "i" was already used in another place..

    If a name is temporarily needed in a certain place and in a certain
    scope then reusing the name shouldn't be a problem.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Thomas Passin on Fri Oct 20 12:43:33 2023
    Thomas Passin <[email protected]> writes:
    If a name is temporarily needed in a certain place and in a certain
    scope then reusing the name shouldn't be a problem.

    It's a question of the mental load. When a scope ends, you are
    relieved of the mental load of keeping in mind all the names that
    are restricted to that scope. Programming is all about readability.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Fri Oct 20 13:48:41 2023
    I still see no great reason for a new feature here and the namespace issue has often been discussed. You can always opt to create your own namespace of some sort and make many of your variables within it and always refer to the variables explicitly so
    the only collisions that can happen are your own carelessness.

    I do note that reusing a variable like "i" is not uncommon and especially when it is merely used as a looping variable. Generally anything else using the same variable does not need to refer to the other use and is in another scope.

    May I politely ask if you can point to other languages that have the feature you want and what it looks like. How does it know when the variable can safely go away? Does it allow you to create your alias in something like a loop and re-assign it or is it
    more like a constant once set and so on?

    If you have a good use case with no other easy way to provide it, you still need to show it is more important than oodles of other feature requests before anyone would consider seriously doing it in some future release.

    I am wondering if your concept of an alias is more typographical than actual. I mean in languages like C, there was often a preprocessor that went through your code and made changes like:

    #DEFINE filename "/usr/me/dir/subdir/file.c"

    This could allow some shorter typing but would not have anything in the namespace on the compiler level which would just see the longer substitutions.

    Could Python include something like this by keeping a table of symbols and replacements or running a pre-processor first? Maybe. But as stated, it does not seem to be a NEED that some feel is important. Assigning a variable to hold a pointer of sorts
    does indeed add to the namespace but the resource involved is not a big deal. Python is an interpreted language that makes one pass but there are languages that make multiple passes through the code and allow things like defining a function after it has
    been called. That is not pythonic.

    -----Original Message-----
    From: Python-list <python-list-bounces+avi.e.gross=[email protected]> On Behalf Of Roel Schroeven via Python-list
    Sent: Friday, October 20, 2023 3:55 AM
    To: [email protected]
    Subject: Re: Where I do ask for a new feature

    Op 20/10/2023 om 5:16 schreef Bongo Ferno via Python-list:
    On Thursday, October 19, 2023 at 11:26:52 PM UTC-3, [email protected] wrote:

    There are many ways to make transient variables that disappear at some time
    and do we need yet another? Yes, you can create one of those ways but what is the big deal with deleting a variable when no longer used?

    Assigning a variable to something can be anything else than a temporal alias. A with statement makes clear that the alias is an alias and is local, and it automatically clears the variable after the block code is used.

    Python clutters the variable space with vars that are needed only on certain places, and an alias doesn't has a scope.
    Convenient alias are short names, and short names are limited in quantity. If the space is cluttered with short alias, it opens risks for wrong utilization.

    Its like writing a "for i" in a list comprehension and having to worry if "i" was already used in another place..
    As long as functions are kept reasonably short, which is a good idea
    anyway, I don't really see any of that as a problem.

    --
    "Experience is that marvelous thing that enables you to recognize a
    mistake when you make it again."
    -- Franklin P. Jones

    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Torrie@21:1/5 to Bongo Ferno via Python-list on Fri Oct 20 12:48:30 2023
    On 10/19/23 19:32, Bongo Ferno via Python-list wrote:

    You can actually just do that with simple assignment!

    short_view = my_object.stuff.long_stuff.sub_object
    print(short_view.some_method())

    but then have to delete the variable manually

    del short_view

    Why? It's just a name in the namespace that you can bind to a function
    object. You can ignore it or rebind it later to something else. There's
    no need to del it, although you can. I'm not sure why you want to del
    it. It's not like a memory leak or something like that.

    I suspect we might also have a misunderstanding of what python variables
    are and how they work, which is why I did not use the word, "reassign"
    but rather "bind" or "rebind."

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Thomas Passin via Python-list on Sat Oct 21 09:44:23 2023
    On 21/10/2023 01.32, Thomas Passin via Python-list wrote:
    On 10/19/2023 11:16 PM, Bongo Ferno via Python-list wrote:
    On Thursday, October 19, 2023 at 11:26:52 PM UTC-3, [email protected]
    wrote:

    There are many ways to make transient variables that disappear at
    some time
    and do we need yet another? Yes, you can create one of those ways but
    what
    is the big deal with deleting a variable when no longer used?

    Assigning a variable to something can be anything else than a temporal
    alias.
    A with statement makes clear that the alias is an alias and is local,
    and it automatically clears the variable after the block code is used.

    Python clutters the variable space with vars that are needed only on
    certain places, and an alias doesn't has a scope.
    Convenient alias are short names, and short names are limited in
    quantity. If the space is cluttered with short alias, it opens risks
    for wrong utilization.

    Its like writing a "for i" in a list comprehension and having to worry
    if "i" was already used in another place..

    If a name is temporarily needed in a certain place and in a certain
    scope then reusing the name shouldn't be a problem.

    Agree. Surely, the only time we use a name like "i" is in a throw-away
    context?

    Under many circumstances Python will let us use "_" in place of a named-identifier - which enables both us and Python to remember its
    short-lived value/local-only use.

    Using an alias MERELY for the convenience of a shorter-name suggests two things: 1 lack of a competent editor/IDE, 2 lack of imagination in
    choosing names (perhaps one of THE skills of programming!)

    Yes, there are other languages which enforce a limited-scope on
    data-items created within or as part of a code-structure - and it IS a
    handy feature! On the other hand, Python's apposite stance can be useful
    too, eg trivial toy-example:

    # list_of_stuff = ...

    for n, element in list_of_stuff:
    if element == target:
    break

    # now element == target, so "element" is probably not that useful
    # but "n" is the index of the target-element, which may be
    # (there are other ways to accomplish same)


    Please take a look at the ideas behind "Modular Programming". This
    encourages the breaking-up of monolithic code and its "cluttered" global namespace, into potentially-independent code-units. The outlined-problem
    is solved by the independent scope of those code-units (in Python:
    modules, classes, functions, and "if __name__ == "__main__":".
    (to say nothing of the coder's virtues of "re-use", the "Single
    Responsibility Principle", "do one thing, and do it well", Law of
    Demeter, ...)

    Personal comment: my habit is to break specs into many classes and
    functions - sometimes more-so than others might prefer. Cannot recall
    when last had that hard-to-locate bug of unwittingly re-using a name/alias. (apologies: not a boast - a recommendation for going modular)

    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)