473,750 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

append to non-existing list

Hello,

if I do this:

for row in sqlsth:
________pkcolum ns.append(row[0].strip())
________etc
without a prior:

pkcolumns = [];
I get this error on first iteration:
UnboundLocalErr or: local variable 'pkcolums' referenced before assignment

I guess that's normal as it's the way python works...?!?

My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don't
know if it's good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves
Nov 9 '05 #1
25 7641
Hi

I think there's no way to append to a non existing list.

Sorry about my question, but the English is my second language, and I
don't know what is the meaning of IHMO (or IMHO). I googled and found
that it means "In My Humbled Opinion", is that true? Thanks and accept
my appologies for not talking entirely about your problem.

Daniel

Nov 9 '05 #2
I am afraid you have to either go back to php or whatever programming
language that fits your style or change your style to fit python.

There is a lot I don't like about python but if you have to use it, you
have to cope with it.

Yves Glodt wrote:
My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don't
know if it's good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves


Nov 9 '05 #3
Yves Glodt wrote:
Hello,

if I do this:

for row in sqlsth:
________pkcolum ns.append(row[0].strip())
________etc
without a prior:

pkcolumns = [];
I get this error on first iteration:
UnboundLocalErr or: local variable 'pkcolums' referenced before assignment

I guess that's normal as it's the way python works...?!?

My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don't
know if it's good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves


You mean you want to type "pkcolumns" only once to keep your code short?
Would something like this be useful?

pkcolumns = [row.strip() for row in sqlsth]
Nov 9 '05 #4

Juho Schultz wrote:
Yves Glodt wrote:
Hello,

if I do this:

for row in sqlsth:
________pkcolum ns.append(row[0].strip())
________etc

You mean you want to type "pkcolumns" only once to keep your code short?
Would something like this be useful?

pkcolumns = [row.strip() for row in sqlsth]


I don't think this is the same as his original code though. There is an
"etc" there. Your version is cleaner and easier to understand but it
can mean another extra pass to go through the result and if sqlsth is
an iterable, it needs to be saved away before iterating it.

Nov 9 '05 #5
bo****@gmail.co m wrote:
I am afraid you have to either go back to php or whatever programming
language that fits your style or change your style to fit python.
sorry for offending... I just asked a question, and now I know one more
thing about python...

And btw I really am surprised by the amount of answers that my question
rose, in so little time!

thanks all!
There is a lot I don't like about python but if you have to use it, you
have to cope with it.

Yves Glodt wrote:
My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don't
know if it's good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves


Nov 9 '05 #6
Juho Schultz wrote:
Yves Glodt wrote:
Hello,

if I do this:

for row in sqlsth:
________pkcolum ns.append(row[0].strip())
________etc
without a prior:

pkcolumns = [];
I get this error on first iteration:
UnboundLocalErr or: local variable 'pkcolums' referenced before assignment

I guess that's normal as it's the way python works...?!?

My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don't
know if it's good to say that here) where I come from (php) I was used
to not-needing it...
regards,
Yves


You mean you want to type "pkcolumns" only once to keep your code short?
Would something like this be useful?

pkcolumns = [row.strip() for row in sqlsth]


I will look into this, maybe it's what I need, thanks!
Nov 9 '05 #7
Yves Glodt wrote:
Hello,

if I do this:

for row in sqlsth:
________pkcolum ns.append(row[0].strip())
________etc
without a prior:

pkcolumns = [];
I get this error on first iteration:
UnboundLocalErr or: local variable 'pkcolums' referenced before assignment

I guess that's normal as it's the way python works...?!?
yes sir.
My question is: Is there no way to append to a non existing list?
No. Definitively. And that's a Good Thing(tm).

How would you use something that doesn't exist ???
I am lazy for declaring it first,
s/declaring/instantiating/

If you were to use your own class Toto, would you ever hope that the
following code would work :

for v in some_seq:
toto.dothis(v)

without instantiating Toto and binding it to the name 'toto' before ?
Well, in Python, a list is an instance of class list. There are
syntactic sugar to instanciate a list (or a tuple or a string or a dict
etc), but this:

my_list = []

is strictly equivalent to this:

my_list = list()

Now let's try something else:

class Machin(object):
def append(self, value): pass

class Bidule(object):
def append(self, value): pass

for i in range(10):
m.append(i)
How should Python interpret this ? Should it create a list, or a Machin,
or a Bidule, or an instance of whatever imported class having a
append() method ?
IMHO it bloats the code,
run your python interpreter and type:
import this

Then read carefully.

Now if being explicit still hurts your personal convictions, there's
this other language with a name starting with p... !-)
and (don't
know if it's good to say that here) where I come from (php) I was used
to not-needing it...


Not creating an Array before using it is Bad Style in PHP (and generate
a Warning BTW).

There are warts in Python (as in any other languages), and there are
things that sometimes bore me but are not really warts. But having to
explicitely instanciate objects can't be seen as a wart in any language
IMHO !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Nov 9 '05 #8
Yves Glodt <y.*****@sitaso ftware.lu> writes:
I guess that's normal as it's the way python works...?!?
Yes, that's the way Python works.
My question is: Is there no way to append to a non existing list?
The next time you go shopping at your local super-market, do
*not* get a shopping-cart (or shopping-basket, or any similar
container). As you pick up the things you want to buy, try
to put them into the non-existing cart. Perhaps you will then
become enlightened.
I am lazy for declaring it first, IMHO it bloats the code, and (don't
know if it's good to say that here) where I come from (php) I was used
to not-needing it...


Basically you want Python to automatically create a list out of
nowhere and bind a variable to that list when you try to access
a variable that doesn't exist in a certain way. How do you
propose that Python should now that it is a *list* you want, and
not some other kind of object?
There actually is a way to do what you want:

for row in sqlsth:
try:
pkcolumns.appen d(row[0].strip())
except NameError:
pkcolumns = [ row[0].strip() ]

However, as you see it is much more work than to do it the right
way. It's also much more fragile; think for example about what
happens if your SQL statement (I assume that's what sqlsth is)
yields zero rows, and you then try to look at pkcolumns after
that loop.
--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"God is real, but Jesus is an integer." ! bellman @ lysator.liu.se
! Make Love -- Nicht Wahr!
Nov 9 '05 #9

Thomas Bellman wrote:
The next time you go shopping at your local super-market, do
*not* get a shopping-cart (or shopping-basket, or any similar
container). As you pick up the things you want to buy, try
to put them into the non-existing cart. Perhaps you will then
become enlightened.


But in PHP(and I believe Perl), it is more like :

"oops, I need a cart. <shout>Hello, I need a cart here, please" and
magically, someone wheel you a cart which you can put your stuff in.
The "@" is the magic calls for service. So as a customer, this sounds
like better service but that will create problems to the super market
as it need extra staff for this service and the environment is noiser,
that is another story.

Nov 9 '05 #10

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

Similar topics

1
3001
by: swaminathang | last post by:
How will I make a text area append only? for eg. if I have some value already initialized for a text area I can add to it but not edit old value. For eg. in a "problem description" text area data can be appended but none of the old details can be tampered with.
3
23956
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
5
4920
by: Barn Yard | last post by:
good morning, im still kind of new to VBA but I have learned some interesting things so far. Right now I am normalizing my survey database so I'm having to run an append query for each question(300 in some surveys). WARNING: I've very new at this so this may not look right. i hope it gives the idea of what I'm trying to accomplish. Before I was just chaning the 1's to 2's then to 3's in the Design view of my query. hope this can save...
2
2712
by: Apple | last post by:
I had create a query with one table and one query, and then create form with that query using wizard, but I can't append or edit data in that form & query. Please help!
3
2348
by: Kevin | last post by:
Hi I have a table that is imported from excel the records are updated as required by the user my question is Can only the new data entered be appended to the table in access ie if i update the table in the morning and import 10 records when i next update the table i want to import only the new data any help would be welcome
0
1418
by: audleman | last post by:
I have an ASP form on my website where a visitor enters information. On submit, the form calls a stored procedure stores in a MS SQL 2000 database. The stored procedure works most of the time, but every once in a while I get an error 3421 (Application uses a value of the wrong type for the current operation) when I try to append the last parameter in the ASP code, which is a Varchar(500). Here's the relevant code: STORED PROCEDURE...
2
12015
by: cb22 | last post by:
I have created a recordset built from scratch, based on data from an external, non-database source. I want to append this recordset to a table in an Access database without using a loop to traverse through each record and add to the table. Any suggestions on what I can use or do to make this possible? Regards.
4
5765
by: pmacdiddie | last post by:
I have an append query that needs to run every time a line item is added to a subform. The append writes to a table that is the source for a pull down box. Problem is that it takes 5 seconds to run. This makes adding lines to an order too slow for the users. The result of the query provides real time availabilty, so I really do need this to work. The tblJobs has only 10,000 records, tblJobsLineItems has 150,000 records.
5
7669
by: jkn | last post by:
Hi all Python 2.4.2 (#1, Apr 26 2006, 23:35:31) on linux2 Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: invalid mode: a
2
2727
by: kvnil | last post by:
Cheers, I've been struggling all day today with this, maybe you could give me some useful pointers. I would like to (elegantly) concatenate a string out of all node values in a map. I have to do it without using boost (as it is forbidden to use it in our project). (Forgive me for giving a non-compiling code.)
0
8999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.