Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2704 > unrolled thread
| Started by | "eryksun ()" <eryksun@gmail.com> |
|---|---|
| First post | 2011-04-06 06:54 -0700 |
| Last post | 2011-04-06 17:12 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
Re: TypeError: iterable argument required "eryksun ()" <eryksun@gmail.com> - 2011-04-06 06:54 -0700
Re: TypeError: iterable argument required Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2011-04-06 08:41 -0700
Re: TypeError: iterable argument required Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2011-04-06 08:57 -0700
Re: TypeError: iterable argument required Blockheads Oi Oi <breamoreboy@yahoo.co.uk> - 2011-04-06 17:12 +0100
| From | "eryksun ()" <eryksun@gmail.com> |
|---|---|
| Date | 2011-04-06 06:54 -0700 |
| Subject | Re: TypeError: iterable argument required |
| Message-ID | <27163982-380f-4ddd-859e-adc069892c28@glegroupsg2000goo.googlegroups.com> |
On Wednesday, April 6, 2011 6:06:06 AM UTC-4, Νικόλαος Κούρας wrote:
>
> The trouble was in `if "@" in mail` .
> You can only test somthing `in` something else if the second thing is
> iterable and None isnt.
>
> So i made the code look like this:
>
> [code]
> if ( mail is not None and '@' in mail ) and comment not in ("Ρωτήστε
> με σχετικά...", "", None):
> [/code]
>
> Now it works like i wanted but i want to ask you if i wrote it
> correctly, especially when i check against `""` and None
You can also use an empty string as the default value when getting the field value, which would simplify your test by eliminating None as a possibility. The particulars will depend on your framework. For example, cgi.FieldStorage has a getfirst method that takes a default value as the 2nd parameter.
Also, a simple OR statement can eliminate the None. For example: mail = mail or ''. Since None is False, the statement returns the right-hand operand, which is an empty string ''.
> And please explain to me the difference betweeen an empty string `""`
> and None. An empty string is still a string with zero characters
> within?
Yes, an empty string is still of type 'str'. None is Python's null value of type 'NoneType'. Its boolean value is False, and it does nothing special and cannot be subclassed. When a function doesn't explicitly return a value, it implicitly returns None.
> > Yes, I made a mistake. It should have been `cursor.execute(SQL_COMMENT_FORM, (mail, comment))`, using a comma instead of a '%' to have it generate SQL string literals from the tuple.
>
> What do you mean by "to have it generate SQL string literals from the
> tuple." Please explain
Here's an amusing warning from the Psycopg (PostgreSQL) docs:
"Warning: Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint."
The line I wrote not only didn't properly quote or escape the data values, but it probably also broke the protection from a SQL injection attack. Always list the data in the 2nd parameter as a tuple.
[toc] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2011-04-06 08:41 -0700 |
| Message-ID | <3aa18971-cbe4-413f-9348-966053a79591@j17g2000vbr.googlegroups.com> |
| In reply to | #2704 |
On 6 Απρ, 16:54, "eryksun ()" <eryk...@gmail.com> wrote: > You can also use an empty string as the default value when getting the field value Please provide me an example. > Also, a simple OR statement can eliminate the None. For example: mail = mail or ''. Since None is False, the statement returns the right-hand operand, which is an empty string ''. >>> mail = None >>> mail >>> mail = mail or '' >>> mail '' Why in 2nd case the returned value of mail is None. Why shouldn't it be the empty string since mail = None which is false. How exactly Python parses those two statements in english words? >>> mail = None >>> mail >>> mail = '' or mail >>> mail >>> > The line I wrote not only didn't properly quote or escape the data values, but it probably also broke the protection from a SQL injection attack. Always list the data in the 2nd parameter as a tuple. Can you please also provide an example of what happens if i use the special formatting identidier `%` instead of a comma?
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2011-04-06 08:57 -0700 |
| Message-ID | <899d22f6-c936-4fe6-b824-4d064281ca54@o26g2000vby.googlegroups.com> |
| In reply to | #2704 |
>>> mail = None >>> mail = mail or 7 >>> mail 7 >>> mail = None >>> mail = 7 or mail >>> mail 7 Here no matter the order iam writing the comparison it always return the number. why not the same here? >>> mail = None >>> mail = mail or '' >>> mail '' >>> mail = None >>> mail = '' or mail >>> mail >>> Why the or operator behaves differently with numbers than from strings? Please explain to me how it parses it with words. Thank you.
[toc] | [prev] | [next] | [standalone]
| From | Blockheads Oi Oi <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2011-04-06 17:12 +0100 |
| Message-ID | <mailman.77.1302106354.9059.python-list@python.org> |
| In reply to | #2707 |
On 06/04/2011 16:57, Νικόλαος Κούρας wrote: >>>> mail = None >>>> mail = mail or 7 >>>> mail > 7 > >>>> mail = None >>>> mail = 7 or mail >>>> mail > 7 > > Here no matter the order iam writing the comparison it always return > the number. > > why not the same here? > >>>> mail = None >>>> mail = mail or '' >>>> mail > '' > >>>> mail = None >>>> mail = '' or mail >>>> mail >>>> > > Why the or operator behaves differently with numbers than from > strings? > > Please explain to me how it parses it with words. > > Thank you. See this for an explanation of Python truth value testing. http://docs.python.org/library/stdtypes.html Cheers. Mark L.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web