On Mon, Sep 17, 2012 at 11:17 AM, <sepa...@sibmail.com> wrote:

> Hello, Vernon Cole.
> Or you probably do not understand the question, or I did not properly
> posed the question.
> I know I can define a function (ttt). And if I'm interested in __
> getattr__, so in this case it can not be determined, or its name and
> parameters are defined at run time.
> Thank you.
>
> The name of a function must be defined somewhere, or it cannot be called.
It can be defined for arbitrary argument lists.  See
http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists

Here is an example...
<code>
from __future__ import print_function
def f(*p, **k):
    print('Your positional arguments were',repr(p))
    print('Your keyword arguments were',repr(k))

print('An empty call...')
f()
print("A call with f(1,'two',spam=3,eggs='four')...")
f(1,'two',spam=3,eggs='four')
</code>

which gives a result as follows...
<result>
C:\Users\vernon\x>"c:\program files (x86)\IronPython 2.7\ipy64" x.py
An empty call...
Your positional arguments were ()
Your keyword arguments were {}
A call with f(1,'two',spam=3,eggs='four')...
Your positional arguments were (1, 'two')
Your keyword arguments were {'eggs': 'four', 'spam': 3}
</result>

The __getattr__ method is defined for class "object".  In order to use it,
you must define a class which overrides the __getattr__ method.
In the code below, I define a class with method "f" like the function
above, which takes an arbitrary argument list. Then I create an instance of
that class called "d".  d.__getattr__ returns an instance of d.f() which
can then be called with arbitrary arguments.

** NOTE:  this is weird code.  It probably should never be used in a
production environment. **
<code>
from __future__ import print_function
class DontKnow(object):
    def f(*p, **k):
        print('Your positional arguments were',repr(p))
        print('Your keyword arguments were',repr(k))
    def __getattr__(self,name):
        print('You tried using',repr(name))
        return self.f

d = DontKnow()
print('An empty call...')
d.f()
print("A call with f(1,'two',spam=3,eggs='four')...")
d.f(1,'two',spam=3,eggs='four')
print("A call to d.g")
d.g
print("A call to d.g(10,20,cheese='chedder')...")
d.g(10,20,cheese='chedder')
</code>
<result>
C:\Users\vernon\x>"c:\program files (x86)\IronPython 2.7\ipy64" x.py
An empty call...
Your positional arguments were (<DontKnow object at 0x000000000000002B>,)
Your keyword arguments were {}
A call with f(1,'two',spam=3,eggs='four')...
Your positional arguments were (<DontKnow object at 0x000000000000002B>, 1,
'two')
Your keyword arguments were {'eggs': 'four', 'spam': 3}
A call to d.g
You tried using 'g'
A call to d.g(10,20,cheese='chedder')...
You tried using 'g'
Your positional arguments were (<DontKnow object at 0x000000000000002B>,
10, 20)
Your keyword arguments were {'cheese': 'chedder'}
</result>

So there you have an arbitrary callable method name with an arbitrary
argument list -- but you still need a defined object (like the DontKnow
instance "d" above) to hook it to.
--
Vernon

> Dear Sir:
> >    Sorry, your question does not seem to make sense.
> > Your example appears to be a simple function call with one keyword
> > argument. To use it, you simply define the optional arguments when you
> > define the function.  See the documentation at:
> >
> http://docs.python.org/tutorial/controlflow.html#more-on-defining-functions
> >
> >  __getattr__ is only used within classes to emulate methods which do not
> > actually exist. That does not appear to be what you are wanting.
> > --
> > Vernon Cole
> >
> > On Mon, Sep 17, 2012 at 4:42 AM, <sepa...@sibmail.com> wrote:
> >
> >> Hello.
> >> For example in the text of the module meets the command:
> >> ttt(4, x = 7)
> >> where and how to define __ getattr__ to handle it?
> >> Thank you.
> >>
>
_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to