Re: How do I say Is this a function?

2008-05-05 Thread Fabiano Sidler

John Henry schrieb:

  exec fct



You don't want this. You want to store the function in a list instead:

l = [ f1, f3, others ]
for i in [0,1]: l[i]()

Greetings,
Fabiano
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-27 Thread Lie
On Apr 27, 11:01 am, John Henry [EMAIL PROTECTED] wrote:
 On Apr 26, 6:08 pm, Martin v. Löwis [EMAIL PROTECTED] wrote:



   def f1():
      print In f1

   def f3():
      print In f3

   def others():
      print In others

   for i in xrange(1,3):
      fct = f%d()%(i+1)
      try:
         exec fct
      except:
         others()

  I'd write that as

  for i in xrange(1,3):
      globals().get(f%d % (i+1), others)()

  Regards,
  Martin

 Perfect.  Works great.  No EXEC.

 You guys are great.

If you just want to avoid exec, why not:

def f1:
print In f1
def f3:
print In f3

class f4(object):
def __init__(self):
print In f4

def others:
print Since all else failed, I'm in others.

f2 = NAF - Not a Function

flist = [f1, f2, f3, f4]
for fct in flist:
try:
fct()
except TypeError:
others()

It's readable, and it's fast if there's just a few hard fault (try-
except works best when it usually succeed and just fails once or
twice), and it's Pythonic too (Easier to ask forgiveness than to ask
permission). The difference between this and the explicit type
checking is that this allows a class (like f4) to pass since a Class
Constructor  Initiator is a callable function too, depending on your
need, you might want to consider class constructor as a function too.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-27 Thread John Henry
On Apr 27, 10:49 am, Lie [EMAIL PROTECTED] wrote:
 On Apr 27, 11:01 am, John Henry [EMAIL PROTECTED] wrote:



  On Apr 26, 6:08 pm, Martin v. Löwis [EMAIL PROTECTED] wrote:

def f1():
   print In f1

def f3():
   print In f3

def others():
   print In others

for i in xrange(1,3):
   fct = f%d()%(i+1)
   try:
  exec fct
   except:
  others()

   I'd write that as

   for i in xrange(1,3):
   globals().get(f%d % (i+1), others)()

   Regards,
   Martin

  Perfect.  Works great.  No EXEC.

  You guys are great.

 If you just want to avoid exec, why not:

 def f1:
 print In f1
 def f3:
 print In f3

 class f4(object):
 def __init__(self):
 print In f4

 def others:
 print Since all else failed, I'm in others.

 f2 = NAF - Not a Function

 flist = [f1, f2, f3, f4]
 for fct in flist:
 try:
 fct()
 except TypeError:
 others()

 It's readable, and it's fast if there's just a few hard fault (try-
 except works best when it usually succeed and just fails once or
 twice), and it's Pythonic too (Easier to ask forgiveness than to ask
 permission). The difference between this and the explicit type
 checking is that this allows a class (like f4) to pass since a Class
 Constructor  Initiator is a callable function too, depending on your
 need, you might want to consider class constructor as a function too.

The reason I didn't want to do that is because when something goes
wrong inside the fcts, others gets executed.  I wanted the program to
crash and burn rather than running others.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-26 Thread Dan Bishop
On Apr 26, 6:17 pm, John Henry [EMAIL PROTECTED] wrote:
 How do I determine is something a function?

 For instance, I don't want to relying on exceptions below:

 def f1():
print In f1

 def f3():
print In f3

 def others():
print In others

 for i in xrange(1,3):
fct = f%d()%(i+1)
try:
   exec fct
except:
   others()

 I wish to say:

if value of fct is a funtion, invoke it, otherwise invoke others().

 Thanks,

hasattr(fct, '__call__')

And be careful about using the exec statement.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-26 Thread [EMAIL PROTECTED]
callable(func) returns whether something is callable(will return true
for classes, functions, and objects with __call__ methods).

On Apr 26, 6:25 pm, Dan Bishop [EMAIL PROTECTED] wrote:
 On Apr 26, 6:17 pm, John Henry [EMAIL PROTECTED] wrote:



  How do I determine is something a function?

  For instance, I don't want to relying on exceptions below:

  def f1():
     print In f1

  def f3():
     print In f3

  def others():
     print In others

  for i in xrange(1,3):
     fct = f%d()%(i+1)
     try:
        exec fct
     except:
        others()

  I wish to say:

     if value of fct is a funtion, invoke it, otherwise invoke others().

  Thanks,

 hasattr(fct, '__call__')

 And be careful about using the exec statement.

--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-26 Thread Martin P. Hellwig

John Henry wrote:

How do I determine is something a function?

For instance, I don't want to relying on exceptions below:

def f1():
   print In f1

def f3():
   print In f3

def others():
   print In others

for i in xrange(1,3):
   fct = f%d()%(i+1)
   try:
  exec fct
   except:
  others()

I wish to say:

   if value of fct is a funtion, invoke it, otherwise invoke others().

Thanks,


One way I would think of is:
str(type(fct)) == type 'function'

--
mph
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-26 Thread John Machin

Dan Bishop wrote:

On Apr 26, 6:17 pm, John Henry [EMAIL PROTECTED] wrote:

How do I determine is something a function?

For instance, I don't want to relying on exceptions below:


And why not?



def f1():
   print In f1

def f3():
   print In f3

def others():
   print In others

for i in xrange(1,3):
   fct = f%d()%(i+1)
   try:
  exec fct
   except:
  others()

I wish to say:

   if value of fct is a funtion, invoke it, otherwise invoke others().

Thanks,


hasattr(fct, '__call__')

And be careful about using the exec statement.


1. In the OP's code, fct is a string, e.g. f2(), so hasattr(fct, 
'__call__') will never return True.


2. Objects other than functions have a __call__ attribute. Some callable 
 objects don't have a __call__ attribute.


3. Use __doubleunderscore__ things only when you need them and only when 
you know what you are doing. There are high-level built-in functions 
such as callable and isinstance that can be used for such tests. The 
literal answer to the OP's question is:


from types import FunctionType
if isinstance(fct, FunctionType):
   do_something()

Something like the following will do the required trick (find callables 
in the global namespace whose name matches a pattern) without using the 
dreaded exec. Note I've left out the logic to call others; the OP's code 
will call others once for each missing function ... let's leave him to 
sort out whether that's a good idea or that should be changed to calling 
it only once if all functions are missing.


 def f1():
...print in f1
...
 def f3():
...print in f3
...
 globdict = globals()
 globdict # output prettied manually
{'f1': function f1 at 0x00BA0470,
 'f3': function f3 at 0x00BA04F0,
 '__builtins__': module '__builtin__' (built-in),
 '__name__': '__main__',
 '__doc__': None}
 for i in xrange(1, 4): ### upper bound 3 probably a bug
... fname = f%d % i
... if fname in globdict:
... func = globdict[fname]
... if callable(func):
... func()
...
in f1
in f3


Cheers,
John
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-26 Thread Martin v. Löwis
 def f1():
print In f1
 
 def f3():
print In f3
 
 def others():
print In others
 
 for i in xrange(1,3):
fct = f%d()%(i+1)
try:
   exec fct
except:
   others()

I'd write that as

for i in xrange(1,3):
globals().get(f%d % (i+1), others)()

Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-04-26 Thread John Henry
On Apr 26, 6:08 pm, Martin v. Löwis [EMAIL PROTECTED] wrote:
  def f1():
     print In f1

  def f3():
     print In f3

  def others():
     print In others

  for i in xrange(1,3):
     fct = f%d()%(i+1)
     try:
        exec fct
     except:
        others()

 I'd write that as

 for i in xrange(1,3):
     globals().get(f%d % (i+1), others)()

 Regards,
 Martin

Perfect.  Works great.  No EXEC.

You guys are great.
--
http://mail.python.org/mailman/listinfo/python-list