what's the best way to call a method of object without a guarantee ofits existence

L

Leon

One way, define the object before it is used,
like this:
object = None
......
......

if object is not None:
object.method()

The other way, using try ... catch
try:
object.method()
catch NameError:
pass

for big programs, which is better, or any other way?

Miles
 
M

Marco Mariani

Leon said:
One way, define the object before it is used,
like this:
object = None

This is a good practice anyway. Conditional existance of objects is
quite evil. Resorting to if defined('foo') is double-plus-ugly.

The other way, using try ... catch
try:
object.method()
catch NameError:
pass

Except you should trap AttributeError because you defined the thing as
None before. NameErrors should be fixed as bugs, not trapped (IMHO --
but in python there is always a use case for everything).

Keep in mind that AttributeError might come from inside the method(),
which could be confusing

By using the

if stuff:
stuff.run()

idiom, you avoid the last issue and keep it simple enough.

for big programs, which is better, or any other way?

Define "big", as in scope, LOCs, or number of committers?
 
L

Leon

This is a good practice anyway. Conditional existance of objects is
quite evil. Resorting to if defined('foo') is double-plus-ugly.

This was why I asked this question. In my program, I use the pattern
of
object.method() a lot, but every time I need to check if it exists or
not,
which makes me feel uncomfortable.

Except you should trap AttributeError because you defined the thing as
None before. NameErrors should be fixed as bugs, not trapped (IMHO --
but in python there is always a use case for everything).

Keep in mind that AttributeError might come from inside the method(),
which could be confusing

By using the

if stuff:
    stuff.run()

idiom, you avoid the last issue and keep it simple enough.


Define "big", as in scope, LOCs, or number of committers?
"big", I mean, is not in the same scope
For instance,


class frame(wx.Frame):
def __init__(self, parent, ....):
....
self.parent = parent # parent from another module
.....

def eventHandler_1(self,event):
if self.parent.object_1 is not None:
self.parent.object_1.method()

def eventHandler_2(self, event):
if self.parent.object_2 is not None:
self.parent.object_2.method()

So I need to go back to the module including "parent" class
to define the objects that I maybe use in future as None,
actually, the module including "parent' class works very well
without those definitions, from parent class's point of view,
those definitions are kind of noisy.

Thanks Marco
 
M

Marco Mariani

Leon said:
So I need to go back to the module including "parent" class
to define the objects that I maybe use in future as None,

You can assign them to a placeholder, with a method that always exists
but does nothing.

class NullObject(object):
def method(self, *args, **kw):
pass

actually, the module including "parent' class works very well
without those definitions, from parent class's point of view,
those definitions are kind of noisy.

If you don't feel the need to define the instances there, maybe they
don't belong to that class after all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top