call 'the following function' using decorators

C

castironpi

I assert it's easier to write:

start_new_thread( this_func )
def thrA():
normal_suite()

than

def thrA():
normal_suite()
start_new_thread( thrA )

If you don't, stop reading. If you do, accomplish it like this:

@decwrap( start_new_thread, Link, ( 2, 3 ) )
def anonfunc( a, b ):
print( a, b )

where 'Link' replaces the following func, plus in keywords too:

@decwrap( Thread, None, target= Link, args= ( 2, 3 ) )
def sampleth( a, b ):
print( a, b )
sampleth.start()
sampleth.join()

'Link' is a pseudo-constant.

Link= object()

@decwrap follows.

def decwrap( func, *ar, **kwar ):
def predec( func2 ):
ar2= list( ar )
while Link in ar2:
ar2[ ar2.index( Link ) ]= func2
kwar2= kwar.copy()
for k, v in kwar2.items():
if v is not Link: continue
kwar2[ k ]= func2
ret= func( *ar2, **kwar2 )
return ret
return predec

Further applications:

@decwrap( button.bind, "<Key-X>", Link )
def anonfunc():
print( 'Key X pressed' )

Optional stylism for readability:

@decwrap( start_new_thread, ~Link, ( 2, 3 ) )
@decwrap( Thread, None, target= ~Link, args= ( 2, 3 ) )
@decwrap( button.bind, "<Key-X>", ~Link )

where 'Link' is:

class NegMarking:
def __invert__( self ): return self

Link= NegMarking()
 
G

Gabriel Genellina

I assert it's easier to write:

start_new_thread( this_func )
def thrA():
normal_suite()

than

def thrA():
normal_suite()
start_new_thread( thrA )

If you don't, stop reading. If you do, accomplish it like this:

@decwrap( start_new_thread, Link, ( 2, 3 ) )
def anonfunc( a, b ):
print( a, b )

And I have to *guess* that start_new_thread is called?
A @threaded decorator might be useful, but the above isn't clear at all.

`import this` inside the interpreter.
 
C

castironpi

En Tue, 12 Feb 2008 15:20:32 -0200, <[email protected]> escribi�:









And I have to *guess* that start_new_thread is called?
A @threaded decorator might be useful, but the above isn't clear at all.

`import this` inside the interpreter.

No new guessing. It's called in

ret= func( *ar2, **kwar2 )

You might suggest a better name, though, than decwrap. Something like
@call_here
with_specified_function. What? This probably goes under "Complex is
better than complicated."

It's not a typical decorator, but the f= g( f ) semantics come handily.
 
C

castironpi

No new guessing.  It's called in

                ret= func( *ar2, **kwar2 )

You might suggest a better name, though, than decwrap.  Something like
@call_here
with_specified_function.  What?  This probably goes under "Complex is
better than complicated."

It's not a typical decorator, but the f= g( f ) semantics come handily.- Hide quoted text -

- Show quoted text -

And Link could do well to call itself Blank/FuncBlank/NextFunc/
something clever
 
C

castironpi

I assert it's easier to write:
Nothing beats if forkthread(): but what are the chances of getting it
in Python?
 
C

castironpi

Nothing beats if forkthread(): but what are the chances of getting it
in Python?

AIR, as I recall, the SML equivalent is:

var TM: thread_manager

in

def forkthread():
TM.add( True, False )

in

if forkthread() thing_to_do() else other_thing_to()


where programs can refer to a thread_manager along with a memory,
which yes, is even in a functional language, well-defined.
thread_manager altertantely beta-reduces one expression at a time in
round-robin succession. thread_manager.add duplicates the current
expression, and evaluates to True in one and False in the other.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top