Random/anonymous class methods

castironpi at gmail.com castironpi at gmail.com
Sun Apr 27 10:17:01 EDT 2008


On Apr 27, 7:11 am, Arnaud Delobelle <arno... at googlemail.com> wrote:
> philly_bob <b0bm0... at gmail.com> writes:
> > In the sample program below, I want to send a random method to a class
> > instance.
> > In other words, I don't know which method to send until run-time.  How
> > can I send ch, which is my random choice, to the myclass instance?
>
> > Thanks,
>
> > Bob=
>
> > ####
> > import random
>
> > class myclass(object):
> >    def meth1(self):
> >       print 'meth1'
> >    def meth2(self):
> >       print 'meth2'
>
> > c=myclass()
> > meths=['meth1', 'meth2']
> > ch=random.choice(meths)
> > c.ch()
>
> This will work:
> getattr(c, ch)()
>
> Getattr(c, "meth1") is equivalent to c.meth1.  Or you could do:
>
> meths = [c.meth1, c.meth2]
> ch = random.choice(meths)
> ch()
>
> --
> Arnaud- Hide quoted text -
>
> - Show quoted text -

MethodType constructors can't be subclassed nor aren't guaranteed
consistent.  Inferrably, it's because there's more than one way to do
it.

The way C used to do it was by ordinal, which turns into Java-style
reflection.  If you use non-linear operation time, string lookup can
actually exceed running value of the dollar that Python is on.  Do you
want the reliability of compile-time errors to do brainwork?  If
you're chasing running time on member look-up, you may be at an Python
entry bottleneck.  The class is the only structure that knows what
function to run on what object.

If you are looking for just a clean -syntax- to denote a uni-
dimensional bit by a string of symbols (write 'that' down), you would
keep methods in synch across *del+setattr*, specifically, changes.

To walk through it: you have a method name from an unknown source, and
you want to know different addresses of functions with that name.

>>> read 'foo'.
'foo' is a method of more than one object.
>>> 'bar'.'foo'
'foo' method of 'bar'.
>>> 'cat'.'foo'
'foo' method of 'cat'
>>> call cat.foo a thousand times

What steps did the interpreter take?

If you mutate 'foo', what should 'cat'.'foo' do?  Strings aren't
immutable in the generic language.

If you mutate 'cat', what should 'cat'.'foo' do?  If you keep a
('cat','foo') pair in memory (which is why I keep metioning two-
dimensional arrays, they're just sparsely populated), you can hash the
intersection to a specific bit, but it may not be faster than hash
lookups on 'cat' and 'foo' combined.  Won't you change the contents of
'cat'?

An object-method pair-wise lookup could make more bucks.  Do you want
more than two?  Isn't a generic n-dimensional lookup from linear
memory a hash table anyway?

You make a case that rigid static lookups are live and useful, and
someone else makes the case that users like dynamic lookups, perhaps a
more even cross-section of them or something.  Rigid statics would be
in hardware.  Can Python live if hardware comes to support it?



More information about the Python-list mailing list