<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>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</div><div><br></div><div>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.</div><div><br></div><div>I forgot about the try / catch approach. I will use this, as I think it's more Pythonic?</div><div><br></div><div>As far as using class, from, or with; overloading existing keywords seems a bit dangerous.</div><div><br></div><div>At this point and the more I think about it, I feel pretty convinced it's a bad idea.</div><br><div><div>On Apr 1, 2010, at 1:54 PM, Bruce Leban wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">I think this is a great idea, but I would not want to introduce a new keyword. How about<div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace"> foo is in bar = hasattr(foo,bar)</font></div>
<div><div><font class="Apple-style-span" face="'courier new', monospace"> foo is not in bar = not hasattr(foo,bar)</font></div><div><br></div></div></div><div>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:</div>
<div><br></div><div><font class="Apple-style-span" face="'courier new', monospace"> foo is class bar = isinstance(foo,bar)</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> foo is from bar = issubclass(foo,bar)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> foo is with bar = type(foo) == type(bar)</font></div><div><div><br></div><div>Can we make an exception to the moratorium, just for today?</div>
<div><br>--- Bruce<br><a href="http://www.vroospeak.com/">http://www.vroospeak.com</a><br>
<br><br><div class="gmail_quote">On Thu, Apr 1, 2010 at 8:15 AM, Robert Madole <span dir="ltr"><<a href="mailto:robmadole@gmail.com">robmadole@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
So I just wrote my 1-gazillionth "if hasattr('method', my_object):" and wanted badly to write this.<br>
<br>
if 'method' on my_object:<br>
<br>
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.<br>
<br>
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.<br>
<br>
Thanks,<br>
<br>
Rob Madole<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</blockquote></div><br></div></div>
</blockquote></div><br></body></html>