473,386 Members | 1,720 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,386 software developers and data experts.

Why the result

Why the third print stement output "'comments': [(1, 2, 3)]", I think
it should be [].
I am using
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32.

# program
class PicInfo:
def __init__(self, intro="", tags="", comments=[]):
self.picintro = intro
self.pictags = tags
self.comments = comments

def addcomment(self, comment):
self.comments.insert(0, comment)

dbdata = PicInfo()
print dbdata.__dict__
dbdata.addcomment((1,2,3))
print dbdata.__dict__
dbdata = PicInfo()
print dbdata.__dict__

# output
{'pictags': '', 'comments': [], 'picintro': ''}
{'pictags': '', 'comments': [(1, 2, 3)], 'picintro': ''}
{'pictags': '', 'comments': [(1, 2, 3)], 'picintro': ''} <- why
comments is not []?

Oct 20 '06 #1
2 1034
"HYRY" <zh*****@feng.co.jpwrote:
Why the third print stement output "'comments': [(1, 2, 3)]", I think
it should be [].
default arguments are calculated once. for details, see the tutorial

http://docs.python.org/tut/node6.htm...00000000000000

and the FAQ:

http://pyfaq.infogami.com/why-are-de...etween-objects

and the language reference:

http://python.org/doc/ref/function.html
http://pyref.infogami.com/def

</F>

Oct 20 '06 #2
HYRY wrote:
Why the third print stement output "'comments': [(1, 2, 3)]", I think
it should be [].
I am using
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32.

# program
class PicInfo:
def __init__(self, intro="", tags="", comments=[]):
This is a FAQ - and one of the most (in)famous Python's gotchas. Default
arguments are eval'd *only once* - when the def statement is eval'd,
which is usually at import time. So using a mutable object as default
value makes this object behaving somehow like a C 'static' local
variable, ie it keeps it value from call to call.

The canonical idiom is to use None instead:

class PicInfo(object):
def __init__(self, intro="", tags="", comments=None):
self.picintro = intro
self.pictags = tags
if comments is None:
comments = []
self.comments = comments

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jerrygarciuh | last post by:
Hi all, I am iterating through a result set to generate a second set of queries but no matter what I do I get the error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result...
2
by: maceo | last post by:
I have a script that will print out the results of a table and make a calculation of a total of one of the columns. See example: <?php /* Database connection */...
4
by: John Davis | last post by:
<html> <body> <Form action="calc.asp" method="post" name="calc"> <P>NUM1: <input type="text" name="num1"> <P>NUM2: <input type="text" name="num2"> <P>RESULT: <input type="text" name="result">...
2
by: swhite76 | last post by:
I have a section of code that adds some double values and gives an incorrect result. This occurs with data that isn't really waht I would call high precision. An example is the following code...
4
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am quite confused on a equation, as following: #include <iostream> int main(){ int i = 2;
3
by: Michael C# | last post by:
Hi all, I'm sending a command via SqlClient, and it returns two result sets. I can successfully read the first result set, but how can I access the second result set? Here's an example of my...
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
9
by: Petr Vileta | last post by:
Hi, I'm new here and excuse me if this question was be here earlier. I have a simple code <html><body> <?php <?php $link = mysql_connect("localhost", "user", "password") or die("Grr: " ....
9
by: JRough | last post by:
I tried to pass the $result from a mysql_query in a url like this line Header("Location:clm_historyXL.php?_result=".$result); but on the redirect location clm_history.php page I get an error on...
3
by: JRough | last post by:
I want to save two variables in a $_SESSION for use in another page: $_SESSION = $mark; $_SESSION = $num; then on the other page I did this to get the value: $mark =$_SESSION; $num =...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.