473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 1032
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,...
1
by: JT | last post by:
im trying to create a function that accepts a sql statement as a parameter, makes a db connection, returns a recordset, and inserts all items from the recordset into a data dictionary - then i want...
1
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
4
by: Noah | last post by:
I have a dictionary that I would like to expand to satisfy a function's agument list. I can used the ** syntax to pass a dictionary, but this only works if each key in the dictionary matches an...
12
by: David W. Thorell | last post by:
I am trying to write a basic spell check function, one which has as its parameters two strings arrays, one is an article from a file source which needs to be checked for valid words, in this case...
0
by: serkan | last post by:
Guys, I am trying to get this password reset functionality wor for me but I am not successful at all. Please somebody help me. I get "Your password could not be reset - please try again later" so I...
4
by: 63q2o4i02 | last post by:
Hi, I'm writing a hand-written recursive decent parser for SPICE syntax parsing. In one case I have one function that handles a bunch of similar cases (you pass the name and the number of...
3
by: craigkenisston | last post by:
I want to use Dictionary class like this: textboxM.Text = oMyDict.GetValue("textboxM"); textboxN.Text = oMyDict.GetValue("textboxN"); Where my dictionary contains the list of values for my...
3
by: Wal | last post by:
I have written a program which lists all possible English words from a set of letters. Trouble is if you enter something over 8 letters long the function can go on for several minutes. I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.