[Python-ideas] "on" statement

Robert Madole robmadole at gmail.com
Thu Apr 1 21:46:30 CEST 2010


I'm new to the list, so give me some time to grok why "new statement/keyword" is a Bad Idea.  I'll refrain from the usual, "It's just a new statement, what's so bad about that" so you guys don't have to scold me.  I suspect the reason relates to bloating the core language :D

I think the in statement already hooks into __contains__.  So I don't think using "foo is in bar" would work.  That already exists and means something else.

I forgot about the try / catch approach.  I will use this, as I think it's more Pythonic?

As far as using class, from, or with; overloading existing keywords seems a bit dangerous.

At this point and the more I think about it, I feel pretty convinced it's a bad idea.

On Apr 1, 2010, at 1:54 PM, Bruce Leban wrote:

> I think this is a great idea, but I would not want to introduce a new keyword. How about
> 
>     foo is in bar     = hasattr(foo,bar)
>     foo is not in bar = not hasattr(foo,bar)
> 
> But there are other common checks I do all the time that I would really like to have shortcuts for. Here are ways to do them without new keywords:
> 
>     foo is class bar = isinstance(foo,bar)
>     foo is from bar  = issubclass(foo,bar)
>     foo is with bar  = type(foo) == type(bar)
> 
> Can we make an exception to the moratorium, just for today?
> 
> --- Bruce
> http://www.vroospeak.com
> 
> 
> On Thu, Apr 1, 2010 at 8:15 AM, Robert Madole <robmadole at gmail.com> wrote:
> So I just wrote my 1-gazillionth "if hasattr('method', my_object):" and wanted badly to write this.
> 
>        if 'method' on my_object:
> 
> I'm not the most experienced Python user, but as far as I can tell hasattr is the preferred way of checking if a method exists.
> 
> Please correct me if there is a better way, otherwise I'd like to see what everyone thinks about an "on" statement being added to the Python language.
> 
> Thanks,
> 
> Rob Madole
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100401/0d3fe165/attachment.html>


More information about the Python-ideas mailing list