[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Irit Katriel via Python-ideas
 

On Sunday, May 23, 2021, 02:23:05 PM GMT+1, Shivam Saini 
 wrote:  
 >> Like the first example in which I am sending an log, which isn't important. 

If the log is not important, then why are you sending it?

  ___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5OSBF3W6MIZJ4TBIVH4DGUPV6GIMQMYN/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Chris Angelico
On Mon, May 24, 2021 at 3:38 AM Marco Sulla
 wrote:
> > > > Do you yearn for actual refactoring tools - which do exist?
> > >
> > > Renaming tools of IDE do not work in 100% of the cases. For example,
> > > if you have _variable in an eval string, it's not replaced.
> >
> > Another reason not to use eval.
>
> I completely agree, but the world is not perfect. Another example is
> the use of the _variable in a f-string.
>

f-strings are code, and should be treated as code by any decent
refactoring tool.

Language features shouldn't be dictated by poor tools, nor by poor codebases.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6O6NDZBOWAI6DK7623HHIC7OESQ46KWV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread Todd
The pytoolz/cytoolz project already has this:
https://toolz.readthedocs.io/en/latest/api.html#toolz.dicttoolz.get_in

On Sun, May 23, 2021, 11:44 Chris Angelico  wrote:

> On Mon, May 24, 2021 at 1:24 AM MRAB  wrote:
> > Also, if the first lookup returns a list or a tuple, and an argument can
> > be an index of that list, would be make sense to add a similar method to
> > lists and tuples?
>
> Or, better: make it a stand-alone function, not a method of anything.
> Although that kinda takes it out of python-ideas territory because it
> could easily be a personal library function instead:
>
> _sentinel = object() # or see other proposals
> def get_deep(obj, *keys, default=_sentinel):
> if len(keys) == 1: keys = list(keys[0])
> try:
> for key in keys:
> obj = obj[key]
> except (LookupError, TypeError): # the OP did include TypeError
> if default is not _sentinel: return default
> raise
> return obj
>
> Done. Personally, I'd just go with "except LookupError:", but this is
> another advantage of personal library functions: you don't have to
> bikeshed them with everyone else :)
>
> ChrisA
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/YM6KCRUJV2ROYV2TC44DWECFJV6TG4A6/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MEQM2SZQD5KPVZHUM655DRR4EV42U63Y/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 17:43, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2021-05-23 at 16:30:35 +0200,
> Marco Sulla  wrote:
>
> > On Sun, 23 May 2021 at 14:50, Chris Angelico  wrote:
> > >
> > > On Sun, May 23, 2021 at 10:42 PM Marco Sulla
> > >  wrote:
> > > >
> > > > On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
> > > > >
> > > > > On Sun, May 23, 2021 at 10:30 PM Marco Sulla
> > > > >  wrote:
> > > > > >
> > > > > > I think the only reason to introduce something like `private` is
> > > > > > refactoring. If you added a `_variable` and later you decided to
> > > > > > expose it, you have to change it to `variable`. This is something 
> > > > > > that
> > > > > > in languages like Java is not necessary, you have only to change the
> > > > > > variable from private to public. This sometimes bothered me in 
> > > > > > Python.
> > > > >
> > > > > Since you started with it private, you should be able to solve this
> > > > > with a simple search-and-replace within the class's own definition.
> > > > > Nothing outside the class should be affected. If it's that hard to
> > > > > replace "self._variable" with "self.variable"
> > > >
> > > > And, in non-trivial cases, it is :)
> > >
> > > I'm curious as to what sort of non-trivial cases you have to deal
> > > with, where you have something harder than search-and-replace
> >
> > For example, having _variable in another class.
>
> Then it isn't/wasn't private.

I'm not saying _variable of the class A I want to rename is used by
the class B. I'm saying that there could exist another class B that
has a _variable with the same name. In this case, a simple search and
replace all will fail.

>
> > > Do you yearn for actual refactoring tools - which do exist?
> >
> > Renaming tools of IDE do not work in 100% of the cases. For example,
> > if you have _variable in an eval string, it's not replaced.
>
> Another reason not to use eval.

I completely agree, but the world is not perfect. Another example is
the use of the _variable in a f-string.

>
> > > Or maybe most Python programmers don't think in terms of private
> > > members, and it's easier to just make something public from the start.
> >
> > N, I see private vars used a lot. It's @property that, it seems to
> > me, is used sporadically.
>
> I suspect that I am not "most Python programmers,", but I always think
> of state (aka members, variables, attributes) as private and internal.
> If I think I need to reach into an application level object for its
> state (to read it or to write it), or that I need a getter, let alone a
> setter, then I'm doing (or have done) somthing else wrong.
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/MDCRGZBOHLTC5DGJFYXGOIJZ2WECT5YL/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RICX4FO3XOEEVVBUZCC6UZB2VUBWUMLG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 17:22, MRAB  wrote:
>
> On 2021-05-23 13:37, Marco Sulla wrote:
> > I propose to add a get_deep(*args, default=_sentinel) method to dict.
> >
> > It can accept a single argument, that must be an iterable, or multiple
> > arguments.
> >
> > The first element must be a key of the dict. If there's not a second
> > element, the value is returned. If it's present, it tries to use it as
> > an argument for the eventual __getitem__() of the value object, and so
> > on.
> >
> > In this process, if a KeyError, an IndexError or a TypeError is
> > raised, if default is set its value is returned, otherwise the
> > exception will be raised.
> >
> > Example:
> >
> > d = {1: [42]}
> > d.get_deep(1, 0)
> > # 42
> > d.get_deep(range(3), default=1981)
> > # 1981
> > d.get_deep((1, 1))
> > # IndexError: list index out of range
> >
> A slight problem here: a tuple (1, 1) can be a key of the dict.

This problem can raise only if you want to get the first level, but
for this purpose there's __getitem__ already.

You can have:

d = {(1, 1): [5, 7]}

and probably you will write

d.get_deep((1, 1), 1)

or

d.get_deep([(1, 1), 1])

>
> Also, if the first lookup returns a list or a tuple, and an argument can
> be an index of that list, would be make sense to add a similar method to
> lists and tuples?
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/XQAFJEZ5PABLKRF7FLDOEUDKNDGF7D34/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EJLZHRMRKPD6WUDFABH6ODFBIUWCRKDF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Serhiy Storchaka
23.05.21 12:42, Shivam Saini пише:
>     except:
>         pass

Don't do this. Never write a bare except handler which does not re-raise
an exception. There are few exceptions of this rule, but it is unlikely
that you will see them in first years of your practice. It is an
anti-pattern, and a feature that facilitates its use will never be added.

Always specify exceptions which you expect to catch. If you want to look
cool, you can use contextlib.suppress(), although it can make further
refactoring more difficult.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RMAQZUPACCB7IZGTIWBQ7DRDPCZ6EN7M/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Serhiy Storchaka
23.05.21 16:22, Shivam Saini пише:
> After all, python is known for one liners

It is not Python that is known for one liners. Python syntax is rather
opposed to one liners. It encourages and sometimes forces a user to
write well-indented code.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/75RY5BWJEJDV36EHIKPWPKQ5BZL5BSWM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Ricky Teachey
On Sun, May 23, 2021, 12:02 PM Damian Shaw 
wrote:

> FYI,
>
> Something very similar already exists in the standard library,
> contextlib.suppress:
> https://docs.python.org/3/library/contextlib.html#contextlib.suppress
>
> It makes a nice 2+ liner for a lot of situations:
>
> with suppress(Exception):
> ...
>
> Seems more flexible than OPs keyword suggestion as you can fit an entire
> block of code in there, not just a single expression. And it's quite
> semantically expressive.
>
> Damian
> (he/him)
>

I agree this is better than a decorator or the OP's idea. Didn't know this
existed but I'll definitely be making use of it.

>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OL26EVXX72QAMCYGS2NDMGJNIFPW4QNR/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Ricky Teachey
On Sun, May 23, 2021, 9:35 AM Stestagg  wrote:

> FYI, default here is unused.
>

Thanks! Yes I had put that at the first and intended to remove it.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FEKTTEBVVNYIO23DVS6ISDU5NWLNGLM6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread 2QdxY4RzWzUUiLuE
On 2021-05-24 at 01:34:29 +1000,
Steven D'Aprano  wrote:

> On Sun, May 23, 2021 at 06:52:38PM +0530, Shivam Saini wrote:
> 
> > After all, python is known for one liners and this would be an another
> > great one liner if implemented.
> 
> Python isn't known for one-liners. You might be thinking of Perl.

I agree.

> Being known for one-liners is a bad thing. It means that your language 
> is famous for being written in an obfuscated, hard to read, hard to 
> maintain, style.

Being known for one-liners can (*can*, not must) mean that my language
has the right features, abstractions, and APIs for common use cases and
task(s) at hand.

Most programs are composed of multiple one-liners.  ;-)

Any program can be one line long if I put all the details into a library
function.  That said, a [standard] library that contains everything is
not a good goal.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/A7UJZXGCSKMZAVFWHXKNX7FCOAVCD33C/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Abdur-Rahmaan Janhangeer
Greetings list,

This whole thread reminds me of a comment of
Miguel Grinberg i remember somewhere when discussing
secrets. Some go for .env some for env variables but he suggested
focusing the attention on not letting people getting access to the
server instead of trying to be clever about hiding secrets.
The whole thread echoes sentiments in the same direction.

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/B4EF67Q5JFDJGYSIUR6CYVZEPC4QAY2R/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Damian Shaw
FYI,

Something very similar already exists in the standard library,
contextlib.suppress:
https://docs.python.org/3/library/contextlib.html#contextlib.suppress

It makes a nice 2+ liner for a lot of situations:

with suppress(Exception):
...

Seems more flexible than OPs keyword suggestion as you can fit an entire
block of code in there, not just a single expression. And it's quite
semantically expressive.

Damian
(he/him)

On Sun, May 23, 2021 at 9:28 AM Ricky Teachey via Python-ideas <
python-ideas@python.org> wrote:

> I think you can already do all of this with a custom exception-swallowing
> decorator function.
>
> Something like this:
>
> from functools import wraps
>
> def swallow(*exceptions, default=Exception, result=None):
> if not exceptions:
> exceptions = Exception,
> def decorator(func):
> @wraps(func)
> def wrapped(*args, **kwargs):
> try:
> return func(*args, **kwargs)
> except exceptions:
> return result
> return wrapped
> return decorator
>
> Then just write:
>
> @swallower()
> def some_func():
> log_to_telegram('This is a not important message. It's okay even if
> it isn\'t delivered.')
>
> For divide by zero errors you need to put the division operation in a
> lambda function:
>
> safe_divide = swallow(DivideByZero, result=0)
> division = safe_divide(lambda: a/b)
>
> I didn't test any of this but it should be working.
>
> On Sun, May 23, 2021, 8:54 AM Shivam Saini  wrote:
>
>> Sometimes, we need to execute a statement, which might throw an exception
>> and we want to return None if it causes an exception. In short, we want to
>> run that command as failsafe mode, so that it doesn't cause abnormal
>> termination in case of any exception.
>>
>> *Approach till now:*
>>
>> def log_to_telegram(text):
>> ''' Suppose you want to send some log to some telegram channel using
>> telegram bot api. It might raise Network Error, or Invalid Bot token error.
>> '''
>> send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a
>> function wrapper for telegram bot api.
>>
>> def some_func():
>> try:
>> log_to_telegram('This is a not important message. It's okay even
>> if it isn\'t delivered.')
>> except:
>> pass
>> # Next work
>>
>> *Suggested approach:*
>>
>> # consider the same log_to_telegram function as defined in previous
>> example
>>
>> def some_func():
>> safe log_to_telegram('This is a not important message. It's okay even
>> if it isn\'t delivered.')
>> # safe keyword ensures that it raises no exception and the process
>> isn't terminated even if log_to_telegram function raises an exception.
>>
>> *Other examples:*
>>
>> *1. By default, it will return None.*
>> *Example: *
>> try:
>>file = open('some_file')
>>except:
>>file = None
>> *Can be rewritten as:*
>>file = safe open('some_file')
>>
>> *2. We can provide a return value explicitly which should be returned in
>> case of some exception.*
>> *Example: *
>> try:
>>division = a/b
>>except:
>>division = 0
>> *Can be rewritten as:*
>> division   = safe(0) a/b
>>
>> *3. We can set what exceptions we want to be 'safed'.*
>> *Example: *
>> try:
>>element = some_list[index]/divisor
>>except ZeroDivisionError:
>>element = 0  # It will make element zero if divisor is zero,
>> but it will not except IndexError in case index is out of range for
>> some_list
>> *Can be rewritten as:*
>>element = safe(0, ZeroDivisionError) some_list[index]/divisor
>>
>> *I think this keyword should be there in python, it will make code short
>> and readable. *
>> ___
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/ORKXAAH2EZFYHJMVU74H5R6BSJVMMER7/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/D5SCJFV3ASC5VJTCQ2ENGCVGALQCOXGT/
Cod

[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread 2QdxY4RzWzUUiLuE
On 2021-05-23 at 16:30:35 +0200,
Marco Sulla  wrote:

> On Sun, 23 May 2021 at 14:50, Chris Angelico  wrote:
> >
> > On Sun, May 23, 2021 at 10:42 PM Marco Sulla
> >  wrote:
> > >
> > > On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
> > > >
> > > > On Sun, May 23, 2021 at 10:30 PM Marco Sulla
> > > >  wrote:
> > > > >
> > > > > I think the only reason to introduce something like `private` is
> > > > > refactoring. If you added a `_variable` and later you decided to
> > > > > expose it, you have to change it to `variable`. This is something that
> > > > > in languages like Java is not necessary, you have only to change the
> > > > > variable from private to public. This sometimes bothered me in Python.
> > > >
> > > > Since you started with it private, you should be able to solve this
> > > > with a simple search-and-replace within the class's own definition.
> > > > Nothing outside the class should be affected. If it's that hard to
> > > > replace "self._variable" with "self.variable"
> > >
> > > And, in non-trivial cases, it is :)
> >
> > I'm curious as to what sort of non-trivial cases you have to deal
> > with, where you have something harder than search-and-replace
> 
> For example, having _variable in another class.

Then it isn't/wasn't private.

> > Do you yearn for actual refactoring tools - which do exist?
> 
> Renaming tools of IDE do not work in 100% of the cases. For example,
> if you have _variable in an eval string, it's not replaced.

Another reason not to use eval.

> > Or maybe most Python programmers don't think in terms of private
> > members, and it's easier to just make something public from the start.
> 
> N, I see private vars used a lot. It's @property that, it seems to
> me, is used sporadically.

I suspect that I am not "most Python programmers,", but I always think
of state (aka members, variables, attributes) as private and internal.
If I think I need to reach into an application level object for its
state (to read it or to write it), or that I need a getter, let alone a
setter, then I'm doing (or have done) somthing else wrong.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MDCRGZBOHLTC5DGJFYXGOIJZ2WECT5YL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread Chris Angelico
On Mon, May 24, 2021 at 1:24 AM MRAB  wrote:
> Also, if the first lookup returns a list or a tuple, and an argument can
> be an index of that list, would be make sense to add a similar method to
> lists and tuples?

Or, better: make it a stand-alone function, not a method of anything.
Although that kinda takes it out of python-ideas territory because it
could easily be a personal library function instead:

_sentinel = object() # or see other proposals
def get_deep(obj, *keys, default=_sentinel):
if len(keys) == 1: keys = list(keys[0])
try:
for key in keys:
obj = obj[key]
except (LookupError, TypeError): # the OP did include TypeError
if default is not _sentinel: return default
raise
return obj

Done. Personally, I'd just go with "except LookupError:", but this is
another advantage of personal library functions: you don't have to
bikeshed them with everyone else :)

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YM6KCRUJV2ROYV2TC44DWECFJV6TG4A6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Steven D'Aprano
On Sun, May 23, 2021 at 06:52:38PM +0530, Shivam Saini wrote:

> After all, python is known for one liners and this would be an another
> great one liner if implemented.

Python isn't known for one-liners. You might be thinking of Perl.

Being known for one-liners is a bad thing. It means that your language 
is famous for being written in an obfuscated, hard to read, hard to 
maintain, style.



-- 
Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WMKBEFPOQGQJPAOALXWD23H77IORWXS6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread MRAB

On 2021-05-23 13:37, Marco Sulla wrote:

I propose to add a get_deep(*args, default=_sentinel) method to dict.

It can accept a single argument, that must be an iterable, or multiple
arguments.

The first element must be a key of the dict. If there's not a second
element, the value is returned. If it's present, it tries to use it as
an argument for the eventual __getitem__() of the value object, and so
on.

In this process, if a KeyError, an IndexError or a TypeError is
raised, if default is set its value is returned, otherwise the
exception will be raised.

Example:

d = {1: [42]}
d.get_deep(1, 0)
# 42
d.get_deep(range(3), default=1981)
# 1981
d.get_deep((1, 1))
# IndexError: list index out of range


A slight problem here: a tuple (1, 1) can be a key of the dict.

Also, if the first lookup returns a list or a tuple, and an argument can 
be an index of that list, would be make sense to add a similar method to 
lists and tuples?

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XQAFJEZ5PABLKRF7FLDOEUDKNDGF7D34/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 14:50, Chris Angelico  wrote:
>
> On Sun, May 23, 2021 at 10:42 PM Marco Sulla
>  wrote:
> >
> > On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
> > >
> > > On Sun, May 23, 2021 at 10:30 PM Marco Sulla
> > >  wrote:
> > > >
> > > > I think the only reason to introduce something like `private` is
> > > > refactoring. If you added a `_variable` and later you decided to
> > > > expose it, you have to change it to `variable`. This is something that
> > > > in languages like Java is not necessary, you have only to change the
> > > > variable from private to public. This sometimes bothered me in Python.
> > >
> > > Since you started with it private, you should be able to solve this
> > > with a simple search-and-replace within the class's own definition.
> > > Nothing outside the class should be affected. If it's that hard to
> > > replace "self._variable" with "self.variable"
> >
> > And, in non-trivial cases, it is :)
>
> I'm curious as to what sort of non-trivial cases you have to deal
> with, where you have something harder than search-and-replace

For example, having _variable in another class.

> Do you yearn for actual refactoring tools - which do exist?

Renaming tools of IDE do not work in 100% of the cases. For example,
if you have _variable in an eval string, it's not replaced.

> Or maybe most Python programmers don't think in terms of private
> members, and it's easier to just make something public from the start.

N, I see private vars used a lot. It's @property that, it seems to
me, is used sporadically.

>
> ChrisA
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/OT54AL5JEPPOK3PK5WACHAMZ6KNFGRKO/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YLBB7WAM2YCEFSCURUHY7TTNWL3NRXTE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 15:30, Thomas Grainger  wrote:
>
> seems a bit like https://www.python.org/dev/peps/pep-0505/
>
> eg `d?[1]?[0]`

No, I do not want to suppress the exception, only to have a way to
access a nested object in a complicate dict, for example a dict
generated by a JSON.

In your example,

d = {1: [42]}
d.get_deep(2, 0)
# KeyError: 2
d = None
d.get_deep(1, 0)
# AttributeError: 'NoneType' object has no attribute 'get_deep'


> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/ZKYLSYU7U673RX6XQLYBSC2HGEAO5NES/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5CPPV3VADIF57Y4PPVZROOHGX45I7U6D/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Stestagg
FYI, default here is unused.

On Sun, 23 May 2021 at 14:29, Ricky Teachey via Python-ideas <
python-ideas@python.org> wrote:

> I think you can already do all of this with a custom exception-swallowing
> decorator function.
>
> Something like this:
>
> from functools import wraps
>
> def swallow(*exceptions, default=Exception, result=None):
> if not exceptions:
> exceptions = Exception,
> def decorator(func):
> @wraps(func)
> def wrapped(*args, **kwargs):
> try:
> return func(*args, **kwargs)
> except exceptions:
> return result
> return wrapped
> return decorator
>
> Then just write:
>
> @swallower()
> def some_func():
> log_to_telegram('This is a not important message. It's okay even if
> it isn\'t delivered.')
>
> For divide by zero errors you need to put the division operation in a
> lambda function:
>
> safe_divide = swallow(DivideByZero, result=0)
> division = safe_divide(lambda: a/b)
>
> I didn't test any of this but it should be working.
>
> On Sun, May 23, 2021, 8:54 AM Shivam Saini  wrote:
>
>> Sometimes, we need to execute a statement, which might throw an exception
>> and we want to return None if it causes an exception. In short, we want to
>> run that command as failsafe mode, so that it doesn't cause abnormal
>> termination in case of any exception.
>>
>> *Approach till now:*
>>
>> def log_to_telegram(text):
>> ''' Suppose you want to send some log to some telegram channel using
>> telegram bot api. It might raise Network Error, or Invalid Bot token error.
>> '''
>> send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a
>> function wrapper for telegram bot api.
>>
>> def some_func():
>> try:
>> log_to_telegram('This is a not important message. It's okay even
>> if it isn\'t delivered.')
>> except:
>> pass
>> # Next work
>>
>> *Suggested approach:*
>>
>> # consider the same log_to_telegram function as defined in previous
>> example
>>
>> def some_func():
>> safe log_to_telegram('This is a not important message. It's okay even
>> if it isn\'t delivered.')
>> # safe keyword ensures that it raises no exception and the process
>> isn't terminated even if log_to_telegram function raises an exception.
>>
>> *Other examples:*
>>
>> *1. By default, it will return None.*
>> *Example: *
>> try:
>>file = open('some_file')
>>except:
>>file = None
>> *Can be rewritten as:*
>>file = safe open('some_file')
>>
>> *2. We can provide a return value explicitly which should be returned in
>> case of some exception.*
>> *Example: *
>> try:
>>division = a/b
>>except:
>>division = 0
>> *Can be rewritten as:*
>> division   = safe(0) a/b
>>
>> *3. We can set what exceptions we want to be 'safed'.*
>> *Example: *
>> try:
>>element = some_list[index]/divisor
>>except ZeroDivisionError:
>>element = 0  # It will make element zero if divisor is zero,
>> but it will not except IndexError in case index is out of range for
>> some_list
>> *Can be rewritten as:*
>>element = safe(0, ZeroDivisionError) some_list[index]/divisor
>>
>> *I think this keyword should be there in python, it will make code short
>> and readable. *
>> ___
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/ORKXAAH2EZFYHJMVU74H5R6BSJVMMER7/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HRY4LIZHVXUAGSRVUDEA3IHBPW3YB7MR/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Shivam Saini
That wont be an oneliner still. We can add decorator to a function,  and
that don't even be very readable. If we can convert that decorator to an
inbuilt keyword that would work as an one liner and would be very readable
too.

On Sun, 23 May 2021, 18:56 Ricky Teachey,  wrote:

> I think you can already do all of this with a custom exception-swallowing
> decorator function.
>
> Something like this:
>
> from functools import wraps
>
> def swallow(*exceptions, default=Exception, result=None):
> if not exceptions:
> exceptions = Exception,
> def decorator(func):
> @wraps(func)
> def wrapped(*args, **kwargs):
> try:
> return func(*args, **kwargs)
> except exceptions:
> return result
> return wrapped
> return decorator
>
> Then just write:
>
> @swallower()
> def some_func():
> log_to_telegram('This is a not important message. It's okay even if
> it isn\'t delivered.')
>
> For divide by zero errors you need to put the division operation in a
> lambda function:
>
> safe_divide = swallow(DivideByZero, result=0)
> division = safe_divide(lambda: a/b)
>
> I didn't test any of this but it should be working.
>
> On Sun, May 23, 2021, 8:54 AM Shivam Saini  wrote:
>
>> Sometimes, we need to execute a statement, which might throw an exception
>> and we want to return None if it causes an exception. In short, we want to
>> run that command as failsafe mode, so that it doesn't cause abnormal
>> termination in case of any exception.
>>
>> *Approach till now:*
>>
>> def log_to_telegram(text):
>> ''' Suppose you want to send some log to some telegram channel using
>> telegram bot api. It might raise Network Error, or Invalid Bot token error.
>> '''
>> send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a
>> function wrapper for telegram bot api.
>>
>> def some_func():
>> try:
>> log_to_telegram('This is a not important message. It's okay even
>> if it isn\'t delivered.')
>> except:
>> pass
>> # Next work
>>
>> *Suggested approach:*
>>
>> # consider the same log_to_telegram function as defined in previous
>> example
>>
>> def some_func():
>> safe log_to_telegram('This is a not important message. It's okay even
>> if it isn\'t delivered.')
>> # safe keyword ensures that it raises no exception and the process
>> isn't terminated even if log_to_telegram function raises an exception.
>>
>> *Other examples:*
>>
>> *1. By default, it will return None.*
>> *Example: *
>> try:
>>file = open('some_file')
>>except:
>>file = None
>> *Can be rewritten as:*
>>file = safe open('some_file')
>>
>> *2. We can provide a return value explicitly which should be returned in
>> case of some exception.*
>> *Example: *
>> try:
>>division = a/b
>>except:
>>division = 0
>> *Can be rewritten as:*
>> division   = safe(0) a/b
>>
>> *3. We can set what exceptions we want to be 'safed'.*
>> *Example: *
>> try:
>>element = some_list[index]/divisor
>>except ZeroDivisionError:
>>element = 0  # It will make element zero if divisor is zero,
>> but it will not except IndexError in case index is out of range for
>> some_list
>> *Can be rewritten as:*
>>element = safe(0, ZeroDivisionError) some_list[index]/divisor
>>
>> *I think this keyword should be there in python, it will make code short
>> and readable. *
>> ___
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CF5OOOSVZJUGFQF7B3EDEPKIRCCEYGQD/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread Thomas Grainger
seems a bit like https://www.python.org/dev/peps/pep-0505/

eg `d?[1]?[0]`
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZKYLSYU7U673RX6XQLYBSC2HGEAO5NES/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Ricky Teachey via Python-ideas
I think you can already do all of this with a custom exception-swallowing
decorator function.

Something like this:

from functools import wraps

def swallow(*exceptions, default=Exception, result=None):
if not exceptions:
exceptions = Exception,
def decorator(func):
@wraps(func)
def wrapped(*args, **kwargs):
try:
return func(*args, **kwargs)
except exceptions:
return result
return wrapped
return decorator

Then just write:

@swallower()
def some_func():
log_to_telegram('This is a not important message. It's okay even if it
isn\'t delivered.')

For divide by zero errors you need to put the division operation in a
lambda function:

safe_divide = swallow(DivideByZero, result=0)
division = safe_divide(lambda: a/b)

I didn't test any of this but it should be working.

On Sun, May 23, 2021, 8:54 AM Shivam Saini  wrote:

> Sometimes, we need to execute a statement, which might throw an exception
> and we want to return None if it causes an exception. In short, we want to
> run that command as failsafe mode, so that it doesn't cause abnormal
> termination in case of any exception.
>
> *Approach till now:*
>
> def log_to_telegram(text):
> ''' Suppose you want to send some log to some telegram channel using
> telegram bot api. It might raise Network Error, or Invalid Bot token error.
> '''
> send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a
> function wrapper for telegram bot api.
>
> def some_func():
> try:
> log_to_telegram('This is a not important message. It's okay even
> if it isn\'t delivered.')
> except:
> pass
> # Next work
>
> *Suggested approach:*
>
> # consider the same log_to_telegram function as defined in previous example
>
> def some_func():
> safe log_to_telegram('This is a not important message. It's okay even
> if it isn\'t delivered.')
> # safe keyword ensures that it raises no exception and the process
> isn't terminated even if log_to_telegram function raises an exception.
>
> *Other examples:*
>
> *1. By default, it will return None.*
> *Example: *
> try:
>file = open('some_file')
>except:
>file = None
> *Can be rewritten as:*
>file = safe open('some_file')
>
> *2. We can provide a return value explicitly which should be returned in
> case of some exception.*
> *Example: *
> try:
>division = a/b
>except:
>division = 0
> *Can be rewritten as:*
> division   = safe(0) a/b
>
> *3. We can set what exceptions we want to be 'safed'.*
> *Example: *
> try:
>element = some_list[index]/divisor
>except ZeroDivisionError:
>element = 0  # It will make element zero if divisor is zero,
> but it will not except IndexError in case index is out of range for
> some_list
> *Can be rewritten as:*
>element = safe(0, ZeroDivisionError) some_list[index]/divisor
>
> *I think this keyword should be there in python, it will make code short
> and readable. *
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ORKXAAH2EZFYHJMVU74H5R6BSJVMMER7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Shivam Saini
That's what I think it should be for.
I know safe open(...) isn't a really good example for this, but I had just
used that for demonstration purposes. Instead what I am saying is that
sometimes we just don't care even if an statement raises exception. Like
the first example in which I am sending an log, which isn't important.
And in any case, the original try except keywords will still be there.

Also I read the similar idea you mentioned, and I guess that is also a good
way to implement it.
We can replace *safe telegram_log("...")* with one liner: *telegram_log("...")
except: None*

After all, python is known for one liners and this would be an another
great one liner if implemented.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RU3LPYKTYSTJCXUCEIUSG2GCBPZAP6UY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Thomas Grainger
sounds very much like https://www.python.org/dev/peps/pep-0463/#rejection-notice

I'm concerned with the `safe` defaulting to a bare `except:` which will also 
catch CancelledError other errors that should be re-raised

also 
```
   file = safe open('some_file')
```

does not provide a way to manage the file with a context manager: 

```
f = safe open("some_file")
if f is None:
# do something
else:
with f:
# do something else
```

seems no improvement  than the current:

```
try:
f = open("some_file")
except OSError:
# do something
else:
with f:
# do something else
```
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7MN4G5INW2CZXVMC7KHHUMXIZ3ZZNDBZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] New Idea: A safe keyword to execute the following statement in a failsafe mode.

2021-05-23 Thread Shivam Saini
Sometimes, we need to execute a statement, which might throw an exception
and we want to return None if it causes an exception. In short, we want to
run that command as failsafe mode, so that it doesn't cause abnormal
termination in case of any exception.

*Approach till now:*

def log_to_telegram(text):
''' Suppose you want to send some log to some telegram channel using
telegram bot api. It might raise Network Error, or Invalid Bot token error.
'''
send_message(CHANNEL, BOT_TOKEN, TEXT) # Suppose send_message is a
function wrapper for telegram bot api.

def some_func():
try:
log_to_telegram('This is a not important message. It's okay even if
it isn\'t delivered.')
except:
pass
# Next work

*Suggested approach:*

# consider the same log_to_telegram function as defined in previous example

def some_func():
safe log_to_telegram('This is a not important message. It's okay even
if it isn\'t delivered.')
# safe keyword ensures that it raises no exception and the process
isn't terminated even if log_to_telegram function raises an exception.

*Other examples:*

*1. By default, it will return None.*
*Example: *
try:
   file = open('some_file')
   except:
   file = None
*Can be rewritten as:*
   file = safe open('some_file')

*2. We can provide a return value explicitly which should be returned in
case of some exception.*
*Example: *
try:
   division = a/b
   except:
   division = 0
*Can be rewritten as:*
division   = safe(0) a/b

*3. We can set what exceptions we want to be 'safed'.*
*Example: *
try:
   element = some_list[index]/divisor
   except ZeroDivisionError:
   element = 0  # It will make element zero if divisor is zero, but
it will not except IndexError in case index is out of range for some_list
*Can be rewritten as:*
   element = safe(0, ZeroDivisionError) some_list[index]/divisor

*I think this keyword should be there in python, it will make code short
and readable. *
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5SEDN6AUM7LZPZZJJZ3RMAKHKHN6N665/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Chris Angelico
On Sun, May 23, 2021 at 10:42 PM Marco Sulla
 wrote:
>
> On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
> >
> > On Sun, May 23, 2021 at 10:30 PM Marco Sulla
> >  wrote:
> > >
> > > I think the only reason to introduce something like `private` is
> > > refactoring. If you added a `_variable` and later you decided to
> > > expose it, you have to change it to `variable`. This is something that
> > > in languages like Java is not necessary, you have only to change the
> > > variable from private to public. This sometimes bothered me in Python.
> >
> > Since you started with it private, you should be able to solve this
> > with a simple search-and-replace within the class's own definition.
> > Nothing outside the class should be affected. If it's that hard to
> > replace "self._variable" with "self.variable"
>
> And, in non-trivial cases, it is :)

I'm curious as to what sort of non-trivial cases you have to deal
with, where you have something harder than search-and-replace, yet you
have to change your mind about whether something's private or public.
Are your private members so poorly named that "._variable" comes up a
lot? Do you yearn for actual refactoring tools - which do exist?

> > then you can always
> > create a property to make it available under both names.
>
> I use property a lot, but I noticed that the majority of programmers
> do not use them. Also asyncio, written by Guido himself, uses normal
> getters and setters. Maybe @property slows down the code?
>

Or maybe most Python programmers don't think in terms of private
members, and it's easier to just make something public from the start.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OT54AL5JEPPOK3PK5WACHAMZ6KNFGRKO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
>
> On Sun, May 23, 2021 at 10:30 PM Marco Sulla
>  wrote:
> >
> > I think the only reason to introduce something like `private` is
> > refactoring. If you added a `_variable` and later you decided to
> > expose it, you have to change it to `variable`. This is something that
> > in languages like Java is not necessary, you have only to change the
> > variable from private to public. This sometimes bothered me in Python.
>
> Since you started with it private, you should be able to solve this
> with a simple search-and-replace within the class's own definition.
> Nothing outside the class should be affected. If it's that hard to
> replace "self._variable" with "self.variable"

And, in non-trivial cases, it is :)

> then you can always
> create a property to make it available under both names.

I use property a lot, but I noticed that the majority of programmers
do not use them. Also asyncio, written by Guido himself, uses normal
getters and setters. Maybe @property slows down the code?

>
> ChrisA
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/L33KIIKBY4ZT7CVJXLGHXMXDCAM7ZQWH/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZHWEL7FV7HD2QAHQVXGLE4BTKIPU27VX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] dict.get_deep()

2021-05-23 Thread Marco Sulla
I propose to add a get_deep(*args, default=_sentinel) method to dict.

It can accept a single argument, that must be an iterable, or multiple
arguments.

The first element must be a key of the dict. If there's not a second
element, the value is returned. If it's present, it tries to use it as
an argument for the eventual __getitem__() of the value object, and so
on.

In this process, if a KeyError, an IndexError or a TypeError is
raised, if default is set its value is returned, otherwise the
exception will be raised.

Example:

d = {1: [42]}
d.get_deep(1, 0)
# 42
d.get_deep(range(3), default=1981)
# 1981
d.get_deep((1, 1))
# IndexError: list index out of range
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/O53OTTDKWPQ7HZCODZSM7ZVNAQ5HVZG6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Chris Angelico
On Sun, May 23, 2021 at 10:30 PM Marco Sulla
 wrote:
>
> I think the only reason to introduce something like `private` is
> refactoring. If you added a `_variable` and later you decided to
> expose it, you have to change it to `variable`. This is something that
> in languages like Java is not necessary, you have only to change the
> variable from private to public. This sometimes bothered me in Python.

Since you started with it private, you should be able to solve this
with a simple search-and-replace within the class's own definition.
Nothing outside the class should be affected. If it's that hard to
replace "self._variable" with "self.variable", then you can always
create a property to make it available under both names.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/L33KIIKBY4ZT7CVJXLGHXMXDCAM7ZQWH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
I think the only reason to introduce something like `private` is
refactoring. If you added a `_variable` and later you decided to
expose it, you have to change it to `variable`. This is something that
in languages like Java is not necessary, you have only to change the
variable from private to public. This sometimes bothered me in Python.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BCYFOTTKO5MO7Q4P72V47Z3QDHJBCOVA/
Code of Conduct: http://python.org/psf/codeofconduct/