Mailing List Archive

Where I do ask for a new feature
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.


--
https://mail.python.org/mailman/listinfo/python-list
Re: Where I do ask for a new feature [ In reply to ]
On Tue, 17 Oct 2023 at 12:55, Bongo Ferno via Python-list
<python-list@python.org> 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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where I do ask for a new feature [ In reply to ]
> 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
RE: Where I do ask for a new feature [ In reply to ]
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=gmail.com@python.org> On
Behalf Of Bongo Ferno via Python-list
Sent: Thursday, October 19, 2023 9:33 PM
To: python-list@python.org
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

--
https://mail.python.org/mailman/listinfo/python-list
Re: Where I do ask for a new feature [ In reply to ]
On Thursday, October 19, 2023 at 11:26:52?PM UTC-3, avi.e...@gmail.com 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..
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where I do ask for a new feature [ In reply to ]
On 19Oct2023 20:16, Bongo Ferno <bongoferno@gmail.com> 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'>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where I do ask for a new feature [ In reply to ]
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, avi.e...@gmail.com 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
Re: Where I do ask for a new feature [ In reply to ]
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, avi.e...@gmail.com 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.

--
https://mail.python.org/mailman/listinfo/python-list
RE: Where I do ask for a new feature [ In reply to ]
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=gmail.com@python.org> On Behalf Of Roel Schroeven via Python-list
Sent: Friday, October 20, 2023 3:55 AM
To: python-list@python.org
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, avi.e...@gmail.com 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

--
https://mail.python.org/mailman/listinfo/python-list
Re: Where I do ask for a new feature [ In reply to ]
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."
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where I do ask for a new feature [ In reply to ]
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, avi.e...@gmail.com
>> 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
--
https://mail.python.org/mailman/listinfo/python-list