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
asking about an ATTRIBUTE:

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  
 
 [ 5 post ] 

 Relevant Pages 

1. counter function : How works the Set attribute function

2. function attributes are like function objects

3. Query on macro definitions within macro definitions within...

4. Function parameter names within a function

5. Recursive function defined within function => NameError

6. Name of function within the function?

7. access functions vs. directly setting attributes

8. Definition Please First Class Lambda Function In Any Language

9. Retrieving the name of a function from within the function

10. Accessing self defined global variables within a function

11. Q: accessing method name from within a c-function

12. Accessing C functions within tcl script

 

 
Powered by phpBB® Forum Software