I have noticed in the book of words that hasattr works by calling getattr
and raising an exception if no such attribute exists. If I need the value
in any case, am I better off using getattr within a try statement myself, or
is there some clever implementation enhancement which makes this a bad idea?
i.e. should I prefer:
if hasattr(self,"d atum"):
datum=getattr(" datum")
else:
datum=None
self.datum=None
over:
try:
datum=self.geta ttr("datum")
except:
self.datum=None
datum=None
The concept of deliberately raising an error is still foreign to this python
newbie, but I really like the trapping facilities. I just worry about the
performance implications and memory usage of such things, especially since
I'm writing for Zope.
And while I'm here: Is there a difference in performance when checking:
datum is None
over:
datum == None
and similarly:
if x is None or y is None:
or:
if None in (x,y):
I appreciate that these are trivial in the extreme, but I seem to be writing
dozens of them, and I may as well use the right one and squeeze what
performance I can.
Many thanks,
Christopher Boomer.
and raising an exception if no such attribute exists. If I need the value
in any case, am I better off using getattr within a try statement myself, or
is there some clever implementation enhancement which makes this a bad idea?
i.e. should I prefer:
if hasattr(self,"d atum"):
datum=getattr(" datum")
else:
datum=None
self.datum=None
over:
try:
datum=self.geta ttr("datum")
except:
self.datum=None
datum=None
The concept of deliberately raising an error is still foreign to this python
newbie, but I really like the trapping facilities. I just worry about the
performance implications and memory usage of such things, especially since
I'm writing for Zope.
And while I'm here: Is there a difference in performance when checking:
datum is None
over:
datum == None
and similarly:
if x is None or y is None:
or:
if None in (x,y):
I appreciate that these are trivial in the extreme, but I seem to be writing
dozens of them, and I may as well use the right one and squeeze what
performance I can.
Many thanks,
Christopher Boomer.
Comment