state machine and a global variable

tuom.larsen at gmail.com tuom.larsen at gmail.com
Sat Dec 15 04:07:17 EST 2007


On Dec 15, 1:50 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 14 Dec 2007 23:06:28 +0100, Bruno Desthuilliers wrote:
> > Now the question is: why do you think it's so important for your users
> > to only see functions ? What's so wrong with:
>
> > from state_machine import *
> > m = get_state_machine()
> > m.set_state(42)
>
> I can't speak for the "only" part, but it is sometimes really convenient
> to have a set of *cough* convenience functions that do the simple stuff
> for you. For example:
>
> import random
> random.random()
>
> is much nicer for the simple cases than:
>
> import random
> prng = random.Random()
> prng.random()
>
> with the advantage that you can still instantiate your own instance if
> you need/want to.
>
> --
> Steven.

I agree, completely!

Ok, I think I'm going to provide both the simplified interface and the
class to instantiate. Thanks all!

Now suppose, this class looks like:

class StateMachine(object):
    def __init__...
    def function0...
    def function1...
    def function2...
    ...

up to many. And later in the library I would like to expose the
simplified interface as well:

_machine = StateMachine()
function0 = _machine.function0
function1 = _machine.function1
function2 = _machine.function2
...

Is there a way to do so in a programmatic way? Like:

_machine = StateMachine()
for function in {every function in _machine}:
    function = _machine.function

Not that it's difficult to copy-paste some lines, I'm just curious and
maybe it would be a tiny bit easier to maintain the library.



More information about the Python-list mailing list