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
Robert Madole 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
Most Python code should just assume the method is present and let the AttributeError fly if it isn't.
If you genuinely need to check for presence of a method, then either catching the AttributeError or using hasattr is fine.
This is a relatively rare requirement though, so the chance of getting dedicated syntax (and a new keyword, no less!) to support it is basically nil.
Cheers, Nick.
On 4/1/2010 11:15 AM, Robert Madole wrote:
So I just wrote my 1-gazillionth "if hasattr('method', my_object):" and wanted badly to write this.
if 'method' on my_object:
If you do not need the superclass search provided by hasattr,
if 'method' in my_object.__dict__:
'__add__' in int.__dict__
True
But I otherwise agree with Nick.
Terry Jan Reedy
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@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@python.org http://mail.python.org/mailman/listinfo/python-ideas
On Thu, Apr 01, 2010 at 11:54:53AM -0700, Bruce Leban wrote:
foo is in bar = hasattr(foo,bar)
foo is in bar
is currently a valid and widely used expression. It means bar.__contains__(foo).
Oleg.
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)
To me, this is asking whether 'foo' is a class. Better would be:
foo in class bar
foo is from bar = issubclass(foo,bar)
Perhaps:
foo from class bar
(although I'm not happy with it.)
foo is with bar = type(foo) == type(bar)
Can we make an exception to the moratorium, just for today?
Only for foolish ideas. :-)
--- Bruce http://www.vroospeak.com
On Thu, Apr 1, 2010 at 8:15 AM, Robert Madole <robmadole@gmail.com mailto:robmadole@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@python.org <mailto:Python-ideas@python.org> http://mail.python.org/mailman/listinfo/python-ideas
Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
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@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@python.org http://mail.python.org/mailman/listinfo/python-ideas
On 2010-04-01 14:04 PM, Oleg Broytman wrote:
On Thu, Apr 01, 2010 at 11:54:53AM -0700, Bruce Leban wrote:
foo is in bar = hasattr(foo,bar)
foo is in bar
is currently a valid and widely used expression. It means bar.__contains__(foo).
No, it's not.
1 is in range(5)
File "<stdin>", line 1 1 is in range(5) ^ SyntaxError: invalid syntax
On Thu, Apr 01, 2010 at 03:06:18PM -0500, Robert Kern wrote:
On 2010-04-01 14:04 PM, Oleg Broytman wrote:
On Thu, Apr 01, 2010 at 11:54:53AM -0700, Bruce Leban wrote:
foo is in bar = hasattr(foo,bar)
foo is in bar
is currently a valid and widely used expression. It means bar.__contains__(foo).
No, it's not.
1 is in range(5)
File "<stdin>", line 1 1 is in range(5) ^ SyntaxError: invalid syntax
Ah, sorry, you are right. But that shows why the proposed syntax is bad - it's too similar to an existing one.
Oleg.
On Thu, Apr 1, 2010 at 8:54 PM, Bruce Leban bruce@leapyear.org 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?
I hope so! I'm tossing in a few more suggestions that would make some idioms more intuitive:
* iterable except for item := (x for x in iterable if x!=item)
* pass from iterable := for i in iterable: pass
* global return value := sys.exit(value)
* import obj in module as name
import module setattr(module, name, obj) del module
* not raise: BLOCK
try: BLOCK except: pass
* with x as is: BLOCK
_x = deepcopy(x) try: BLOCK finally: x = _x; del _x
* try for i in iterable: BLOCK1 else: BLOCK2
for i in iterable: try: BLOCK1; break except: pass else: BLOCK2
George
On 4/1/2010 3:46 PM, Robert Madole wrote:
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
Yes, plus any keyword we would like to add is almost surely already being using as an indentifier by some code, which would almost surely be broken.
Am 01.04.2010 22:54, schrieb George Sakkis:
I hope so! I'm tossing in a few more suggestions that would make some idioms more intuitive:
import obj in module as name
import module setattr(module, name, obj) del module
My favorite!
Georg
Bruce Leban wrote:
foo is in bar = hasattr(foo,bar) foo is not in bar = not hasattr(foo,bar) foo is class bar = isinstance(foo,bar) foo is from bar = issubclass(foo,bar) foo is with bar = type(foo) == type(bar)
+1. Also we must absolutely have
foo is totally bar
for those *$&@*&(*@& moments we all experience while coding.
On Sun, Apr 4, 2010 at 05:35, Greg Ewing greg.ewing@canterbury.ac.nzwrote:
+1. Also we must absolutely have
foo is totally bar
for those *$&@*&(*@& moments we all experience while coding.
Indeed. On that note, I think I'm going to start implementing Google's recently-announced Google Annotations Gallery as Python decorators. I actually did once implement their @noop...
http://google-opensource.blogspot.com/2010/03/google-annotations-gallery.htm...
George Sakkis, 01.04.2010 22:54:
with x as is: BLOCK
_x = deepcopy(x) try: BLOCK finally: x = _x; del _x
I think this use case is important enough to implement it immediately. However, since the moratorium is currently in place for Python, would it be ok to fork Python 3 to integrate it into the language? I propose Mython as the name of the fork. I also propose to drop the entire Subversion history, so that we can start using Mercurial instantaneously. That will greatly improve the freedom of Mython as a language.
Stefan
On 2010-04-04 14:17 , Stefan Behnel wrote:
George Sakkis, 01.04.2010 22:54:
- with x as is:
BLOCK
_x = deepcopy(x) try: BLOCK finally: x = _x; del _x
I think this use case is important enough to implement it immediately. However, since the moratorium is currently in place for Python, would it be ok to fork Python 3 to integrate it into the language? I propose Mython as the name of the fork. I also propose to drop the entire Subversion history, so that we can start using Mercurial instantaneously. That will greatly improve the freedom of Mython as a language.
The name Mython is taken:
However, you can use the existing Mython to implement this syntax pretty easily.