why function got dictionary

A

AlFire

Hi,

I am seeking an explanation for following:

Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.{}

Q: why function got dictionary? What it is used for?


Thx, Andy
 
D

Diez B. Roggisch

AlFire said:
Hi,

I am seeking an explanation for following:

Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.{}

Q: why function got dictionary? What it is used for?

because it is an object, and you can do e.g.

g.exposed = True

or similar stuff.

Diez
 
B

bruno.desthuilliers

Hi,

I am seeking an explanation for following:

Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.{}

Q: why function got dictionary? What it is used for?

A: everything (or almost) in Python is an object. Including functions,
classes, modules etc.
 
D

Diez B. Roggisch

AlFire said:
you mean an object in the following sense?


Yes.


where could I read more about that?

I don't know. But essentially _everything_ in python is an object. Some of
them lack a __dict__ - e.g. int and float and such - for optimization
reasons. But apart from that, you can treat everything as an object.

Diez
 
T

Tim Roberts

AlFire said:
you mean an object in the following sense?

True

where could I read more about that?

This is a very useful feature. As just one example, the CherryPy web
server framework lets you define a class to handle one directory in a web
site, where each page is mapped to member functions. However, you also
need to include support functions that should not be exposed as web pages.
To do that, you just add an "exposed" attribute to the function:

class MyWebPage:
...
def index( self, ... ):
pass
index.exposed = 1

def notExposed( self, ... ):
pass

def request( self, ... ):
pass
request.exposed = 1
 
C

Christian Heimes

A: everything (or almost) in Python is an object. Including functions,
classes, modules etc.

Everything you can access from or through Python code must be an object.
Every object has at least a type and a reference count.

Christian
 
S

sturlamolden

Q: why function got dictionary? What it is used for?

As previously mentioned, a function has a __dict__ like (most) other
objects.

You can e.g. use it to create static variables:

int foobar()
{
static int i = 0;
return i++;
}

is roughly equivalent to:

def foobar():
foobar.i += 1
return foobar.i
foobar.i = 0
 
B

bruno.desthuilliers

As previously mentioned, a function has a __dict__ like (most) other
objects.

You can e.g. use it to create static variables:

int foobar()
{
static int i = 0;
return i++;

}

is roughly equivalent to:

def foobar():
foobar.i += 1
return foobar.i
foobar.i = 0

barfoo = foobar
foobar = lambda x : x

And boom.

'static' variables are better implemented using either closures,
mutable default arguments or custom callable types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top