Functions attributes accessed from within the function definition URGENT please
Author Message
Functions attributes accessed from within the function definition URGENT please

Hi!

I have a function f that will have an attribute x: f.x = 31426 (for
example).

When defining the function f, I'd like to give this attribute a
default value, so I'll never get an error when trying to access it with f.x,
how can I do this (setting the default value)? O:-)

TIA

'Fernando

Sun, 16 Nov 2003 22:24:56 GMT
Functions attributes accessed from within the function definition URGENT please
is this what you are after?

def f(x=31426):
pass

Quote:
> Hi!

> I have a function f that will have an attribute x: f.x = 31426 (for
> example).

> When defining the function f, I'd like to give this attribute a
> default value, so I'll never get an error when trying to access it with
f.x,
> how can I do this (setting the default value)? O:-)

> TIA

> 'Fernando

Sun, 16 Nov 2003 22:20:37 GMT
Functions attributes accessed from within the function definition URGENT please

Quote:

>is this what you are after?

>def f(x=31426):
>    pass

No. I can't pass it as an argument, because this attribute must change after a
series of function calls and before andother serie of function calls.

'Fernando

Sun, 16 Nov 2003 22:52:45 GMT
Functions attributes accessed from within the function definition URGENT please

Quote:
> is this what you are after?

> def f(x=31426):
>     pass

I think this would be an ARGUMENT, while Fernando is

Quote:

> > Hi!

> > I have a function f that will have an attribute x: f.x = 31426 (for
> > example).

> > When defining the function f, I'd like to give this attribute a
> > default value, so I'll never get an error when trying to access it with

and then the answer is, e.g.:

def f(whatever):
return 3*f.x
f.x = 31426

I.e., set f's attributes with normal syntax just AFTER the
def statement that binds the name f to the function object.

Alex

Sun, 16 Nov 2003 23:11:09 GMT
Functions attributes accessed from within the function definition URGENT please

Quote:
> Hi!

>    I have a function f that will have an attribute x: f.x = 31426 (for
> example).

>    When defining the function f, I'd like to give this attribute a
> default value, so I'll never get an error when trying to access it with f.x,
> how can I do this (setting the default value)? O:-)

Well, why not put the f.x = 31426 right after the definition?
Quote:
>>> def f():

...   print f.x
...
Quote:
>>> f.x=31426
>>> f()

31426

/Fredrik

Mon, 17 Nov 2003 00:04:48 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages