Re: [Tutor] atr in dir Vs. hasattr

2011-03-16 Thread Tim Johnson
* Steven D'Aprano [110316 05:26]: > Tim Johnson wrote: > > What is the difference between using > > hasattr(object, name) > > and > > name in dir(object) > > ? > > Did you read the Fine Manual? No but I will :) > http://docs.python.org/library/functions.html#dir > > "The default dir() mechanis

Re: [Tutor] atr in dir Vs. hasattr

2011-03-16 Thread Steven D'Aprano
Tom Zych wrote: I suppose there must be some reliable way to get a list of *all* an object's attributes, but I don't see it. Nope, there isn't, because Python allows classes to define arbitrary attributes at runtime. >>> import random >>> class Funny: ... def __getattr__(self, name): .

Re: [Tutor] atr in dir Vs. hasattr

2011-03-16 Thread Tom Zych
On Thu, 17 Mar 2011 00:12 +1100, "Steven D'Aprano" wrote: > And here's an example: > > >>> class C(object): > ... pass > ... > >>> hasattr(C, '__eq__') > True > >>> '__eq__' in dir(C) > False OTOH, I got True in 3.1.1. As the docs say, detailed behavior may change across releases. I supp

Re: [Tutor] atr in dir Vs. hasattr

2011-03-16 Thread Steven D'Aprano
Wayne Werner wrote: However, rare is the occasion that you should use either of these. If you're doing something like: if hasattr(myobj, 'something'): myobj.something() else: print "blah blah blah" then what you really should be doing is: try: myobj.something() except AttributeErro

Re: [Tutor] atr in dir Vs. hasattr

2011-03-16 Thread Steven D'Aprano
Tim Johnson wrote: What is the difference between using hasattr(object, name) and name in dir(object) ? Did you read the Fine Manual? http://docs.python.org/library/functions.html#dir "The default dir() mechanism behaves differently with different types of objects, as it attempts to pro

[Tutor] atr in dir Vs. hasattr

2011-03-15 Thread Tim Johnson
This following post was originally posted to the wrong thread. I am reposting (hopefully correctly) with the first and very succint response. I thing the answer is a revealation to be noted: ## On Tue, Mar 15, 2011 at 8:00 PM, Tim Johnson wr

Re: [Tutor] atr in dir Vs. hasattr

2011-03-15 Thread Tim Johnson
* Wayne Werner [110315 17:29]: > On Tue, Mar 15, 2011 at 8:00 PM, Tim Johnson wrote: > > > What is the difference between using > > hasattr(object, name) > > and > > name in dir(object) > > > > hasattr is basically > > try: > object.name > return True > except AttributeError: >

Re: [Tutor] atr in dir Vs. hasattr

2011-03-15 Thread Wayne Werner
On Tue, Mar 15, 2011 at 8:00 PM, Tim Johnson wrote: > What is the difference between using > hasattr(object, name) > and > name in dir(object) > hasattr is basically try: object.name return True except AttributeError: return False while "name in dir(object)" is (AFAIK) more lik

[Tutor] atr in dir Vs. hasattr

2011-03-15 Thread Tim Johnson
What is the difference between using hasattr(object, name) and name in dir(object) ? TIA -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription