470,848 Members | 2,199 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

why function got dictionary

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.
>>def g(): return
....
>>g.__dict__
{}

Q: why function got dictionary? What it is used for?
Thx, Andy
Jun 27 '08 #1
10 936
AlFire wrote:
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.
>>def g(): return
...
>>g.__dict__
{}

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
Jun 27 '08 #2
On 17 avr, 16:06, AlFire <spamgrinder.tryla...@gmail.comwrote:
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.
>>def g(): return
...
>>g.__dict__
{}

Q: why function got dictionary? What it is used for?
A: everything (or almost) in Python is an object. Including functions,
classes, modules etc.
Jun 27 '08 #3
Diez B. Roggisch wrote:
>>
Q: why function got dictionary? What it is used for?

because it is an object, and you can do e.g.
you mean an object in the following sense?
>> isinstance(g,object)
True
where could I read more about that?

Andy
Jun 27 '08 #4
AlFire wrote:
Diez B. Roggisch wrote:
>>>
Q: why function got dictionary? What it is used for?

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

you mean an object in the following sense?
>> isinstance(g,object)
True
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
Jun 27 '08 #5
AlFire <sp******************@gmail.comwrote:
>Diez B. Roggisch wrote:
>>>
Q: why function got dictionary? What it is used for?

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

you mean an object in the following sense?
> isinstance(g,object)
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
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #6
br*****************@gmail.com schrieb:
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

Jun 27 '08 #7
On Apr 17, 4:06 pm, AlFire <spamgrinder.tryla...@gmail.comwrote:
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



Jun 27 '08 #8
On 19 avr, 19:39, sturlamolden <sturlamol...@yahoo.nowrote:
On Apr 17, 4:06 pm, AlFire <spamgrinder.tryla...@gmail.comwrote:
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
barfoo = foobar
foobar = lambda x : x

And boom.

'static' variables are better implemented using either closures,
mutable default arguments or custom callable types.
Jun 27 '08 #9
On Apr 19, 8:33 pm, "bruno.desthuilli...@gmail.com"
<bruno.desthuilli...@gmail.comwrote:
barfoo = foobar
foobar = lambda x : x

And boom.
That's why I used the qualifier 'roughly equivalent' and not simply
'equivalent'.
Jun 27 '08 #10
En Thu, 17 Apr 2008 11:06:08 -0300, AlFire <sp******************@gmail.comescribió:
Q: why function got dictionary? What it is used for?
If you want more details, see PEP 232 that introduced function writable attributes in Python 2.1: http://www.python.org/dev/peps/pep-0232/

--
Gabriel Genellina

Jun 27 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

34 posts views Thread by Jacek Generowicz | last post: by
4 posts views Thread by Noah | last post: by
12 posts views Thread by David W. Thorell | last post: by
3 posts views Thread by craigkenisston | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.