generator testing and attrs

Mike vimakefile at yahoo.com
Fri Oct 10 18:04:58 EDT 2003


"Miki Tebeka" <mikit at zoran.co.il> wrote in message
> You'll see that g has many attributes that f doesnt. Maybe you can use one
of them.

Hmm - doesn't seem so here:

>>> def g(): yield None
>>> dir(g)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict',
'func_doc', 'func_globals', 'func_name']
>>> def f(): return None
>>> dir(f)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict',
'func_doc', 'func_globals', 'func_name']
>>>

> IMO a better way is letting the caller passing you ONLY functions.
> In case of a generator if should pass the .next of the generator.
> ...code...

As I said, I can't do that becuase I'm saving the items to be scheduled
(evaluated lazily) -- I don't want to call f() or f.next() right right away.
(The code sample could have included the comment "# much later..." between
getting the funarg and calling it.)

Ahh -- Looks like the original PEP (255) held the key:

>>> hex(f.func_code.co_flags)
'0x43'
>>> hex(g.func_code.co_flags)
'0x63'
>>>

---
mike






More information about the Python-list mailing list