Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2855 > unrolled thread
| Started by | candide <candide@free.invalid> |
|---|---|
| First post | 2011-04-08 18:26 +0200 |
| Last post | 2011-04-09 07:57 +1000 |
| Articles | 20 on this page of 26 — 15 participants |
Back to article view | Back to comp.lang.python
Argument of the bool function candide <candide@free.invalid> - 2011-04-08 18:26 +0200
Re: Argument of the bool function Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-04-08 12:41 -0400
Re: Argument of the bool function candide <candide@free.invalid> - 2011-04-10 14:54 +0200
Re: Argument of the bool function Chris Angelico <rosuav@gmail.com> - 2011-04-10 23:02 +1000
Re: Argument of the bool function Mel <mwilson@the-wire.com> - 2011-04-10 12:21 -0400
Re: Argument of the bool function "Colin J. Williams" <cjw@ncf.ca> - 2011-04-10 13:51 -0400
Re: Argument of the bool function Ben Finney <ben+python@benfinney.id.au> - 2011-04-11 09:46 +1000
Re: Argument of the bool function Ethan Furman <ethan@stoneleaf.us> - 2011-04-11 16:00 -0700
Re: Argument of the bool function Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-25 16:29 +0200
Re: Argument of the bool function Chris Angelico <rosuav@gmail.com> - 2011-04-26 05:33 +1000
Re: Argument of the bool function Paul Rubin <no.email@nospam.invalid> - 2011-04-25 16:26 -0700
Re: Argument of the bool function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-26 02:38 +0000
Re: Argument of the bool function Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-25 23:28 +0200
Re: Argument of the bool function Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-25 17:44 -0600
Re: Argument of the bool function Mel <mwilson@the-wire.com> - 2011-04-08 16:42 +0000
Re: Argument of the bool function Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-08 10:43 -0600
Re: Argument of the bool function candide <candide@free.invalid> - 2011-04-08 23:32 +0200
Re: Argument of the bool function Ethan Furman <ethan@stoneleaf.us> - 2011-04-08 15:03 -0700
Re: Argument of the bool function candide <candide@free.invalid> - 2011-04-09 00:59 +0200
Re: Argument of the bool function Lie Ryan <lie.1296@gmail.com> - 2011-04-09 15:45 +1000
Re: Argument of the bool function Grant Edwards <invalid@invalid.invalid> - 2011-04-10 03:35 +0000
Re: Argument of the bool function rusi <rustompmody@gmail.com> - 2011-04-09 21:15 -0700
Re: Argument of the bool function Robert Kern <robert.kern@gmail.com> - 2011-04-10 01:22 -0500
Re: Argument of the bool function Robert Kern <robert.kern@gmail.com> - 2011-04-09 18:22 -0500
Re: Argument of the bool function candide <candide@free.invalid> - 2011-04-10 02:33 +0200
Re: Argument of the bool function Ben Finney <ben+python@benfinney.id.au> - 2011-04-09 07:57 +1000
Page 1 of 2 [1] 2 Next page →
| From | candide <candide@free.invalid> |
|---|---|
| Date | 2011-04-08 18:26 +0200 |
| Subject | Argument of the bool function |
| Message-ID | <4d9f374b$0$12803$426a34cc@news.free.fr> |
About the standard function bool(), Python's official documentation tells us the following : bool([x]) Convert a value to a Boolean, using the standard truth testing procedure. In this context, what exactly a "value" is referring to ? For instance, >>> x=42 >>> bool(x=5) True >>> but _expression_ : x=42 has no value.
[toc] | [next] | [standalone]
| From | Benjamin Kaplan <benjamin.kaplan@case.edu> |
|---|---|
| Date | 2011-04-08 12:41 -0400 |
| Message-ID | <mailman.154.1302280920.9059.python-list@python.org> |
| In reply to | #2855 |
On Fri, Apr 8, 2011 at 12:26 PM, candide <candide@free.invalid> wrote: > About the standard function bool(), Python's official documentation tells us > the following : > > bool([x]) > Convert a value to a Boolean, using the standard truth testing procedure. > > > In this context, what exactly a "value" is referring to ? > > > For instance, > > >>>> x=42 >>>> bool(x=5) > True >>>> > > > but _expression_ : > > x=42 > > > has no value. > That's because bool(x=5) isn't doing what you think. >>> bool(y=5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'y' is an invalid keyword argument for this function bool(x=5) is just passing the value 5 as the argument "x" to the function. "value" means just what you'd think- any constant or any value that's been assigned to. > > > > > > -- > http://mail.python.org/mailman/listinfo/python-list >
[toc] | [prev] | [next] | [standalone]
| From | candide <candide@free.invalid> |
|---|---|
| Date | 2011-04-10 14:54 +0200 |
| Message-ID | <4da1a8b5$0$23679$426a74cc@news.free.fr> |
| In reply to | #2857 |
Le 08/04/2011 18:41, Benjamin Kaplan a écrit : > bool(x=5) is just passing the value 5 as the argument "x" to the function. > Anyway, passing x as a keyword argument to the bool function appears to be very rare : i did a regexp search for about 30000 source-code Python files (among them official Python source-code, Django, Sphinx, Eric source-code and many more sources of valuable Python code) and I didn't find even one.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-04-10 23:02 +1000 |
| Message-ID | <mailman.190.1302440567.9059.python-list@python.org> |
| In reply to | #2941 |
On Sun, Apr 10, 2011 at 10:54 PM, candide <candide@free.invalid> wrote: > Anyway, passing x as a keyword argument to the bool function appears to be > very rare : i did a regexp search for about 30000 source-code Python files > (among them official Python source-code, Django, Sphinx, Eric source-code > and many more sources of valuable Python code) and I didn't find even one. Who would use keyword arguments with a function that takes only one arg anyway? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mel <mwilson@the-wire.com> |
|---|---|
| Date | 2011-04-10 12:21 -0400 |
| Message-ID | <inslea$p6d$1@speranza.aioe.org> |
| In reply to | #2942 |
Chris Angelico wrote:
> Who would use keyword arguments with a function that takes only one arg
> anyway?
It's hard to imagine. Maybe somebody trying to generalize function calls
(trying to interpret some other language using a python program?)
# e.g. input winds up having the effect of ..
function = bool
name = 'x'
value = 'the well at the end of the world'
## ...
actions.append ((function, {name:value}))
## ...
for function, args in actions:
results.append (function (**args))
Not something I, for one, do every day. But regularity in a language is
good when you can get it, especially for abstract things like that.
I can sort of guess that `dir` was perhaps coded in C for speed and doesn't
spend time looking for complicated argument lists.
Python is a pragmatic language, so all the rules come pre-broken.
Mel.
[toc] | [prev] | [next] | [standalone]
| From | "Colin J. Williams" <cjw@ncf.ca> |
|---|---|
| Date | 2011-04-10 13:51 -0400 |
| Message-ID | <mailman.196.1302457887.9059.python-list@python.org> |
| In reply to | #2950 |
On 10-Apr-11 12:21 PM, Mel wrote:
> Chris Angelico wrote:
>
>> Who would use keyword arguments with a function that takes only one arg
>> anyway?
>
> It's hard to imagine. Maybe somebody trying to generalize function calls
> (trying to interpret some other language using a python program?)
>
> # e.g. input winds up having the effect of ..
> function = bool
> name = 'x'
> value = 'the well at the end of the world'
> ## ...
> actions.append ((function, {name:value}))
> ## ...
> for function, args in actions:
> results.append (function (**args))
>
> Not something I, for one, do every day. But regularity in a language is
> good when you can get it, especially for abstract things like that.
>
> I can sort of guess that `dir` was perhaps coded in C for speed and doesn't
> spend time looking for complicated argument lists.
>
> Python is a pragmatic language, so all the rules come pre-broken.
>
>
> Mel.
This thread has lasted 3 days so far.
I presume that it is agreed they the following is a satisfactory outcome:
*** Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
(Intel)] on win32. ***
>>> bool(x=0)
False
>>> bool(x=1)
True
>>>
Colin W.
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-04-11 09:46 +1000 |
| Message-ID | <87pqot655h.fsf@benfinney.id.au> |
| In reply to | #2950 |
Mel <mwilson@the-wire.com> writes: > Python is a pragmatic language, so all the rules come pre-broken. +1 QOTW -- \ “Science shows that belief in God is not only obsolete. It is | `\ also incoherent.” —Victor J. Stenger, 2001 | _o__) | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-04-11 16:00 -0700 |
| Message-ID | <mailman.227.1302562142.9059.python-list@python.org> |
| In reply to | #2950 |
Mel wrote: > Python is a pragmatic language, so all the rules come pre-broken. +1 QOTW
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2011-04-25 16:29 +0200 |
| Message-ID | <ip40gf$i0o$1@r03.glglgl.eu> |
| In reply to | #2950 |
Am 10.04.2011 18:21, schrieb Mel:
> Chris Angelico wrote:
>
>> Who would use keyword arguments with a function that takes only one arg
>> anyway?
>
> It's hard to imagine. Maybe somebody trying to generalize function calls
> (trying to interpret some other language using a python program?)
>
> # e.g. input winds up having the effect of ..
> function = bool
> name = 'x'
> value = 'the well at the end of the world'
> ## ...
> actions.append ((function, {name:value}))
> ## ...
> for function, args in actions:
> results.append (function (**args))
Wrong structure.
Better do
function = bool
value = 'the well at the end of the world'
## ...
actions.append((function, (value,), {}))
## ...
for function, args, kwargs in actions:
results.append(function(*args, **kwargs))
or maybe even better (taking care for closures):
function = bool
value = 'the well at the end of the world'
## ...
actions.append(lambda val=value: function(val))
## ...
for function in actions:
results.append(function())
>
> Not something I, for one, do every day. But regularity in a language is
> good when you can get it, especially for abstract things like that.
>
> I can sort of guess that `dir` was perhaps coded in C for speed and doesn't
> spend time looking for complicated argument lists.
>
> Python is a pragmatic language, so all the rules come pre-broken.
>
>
> Mel.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-04-26 05:33 +1000 |
| Message-ID | <mailman.814.1303759995.9059.python-list@python.org> |
| In reply to | #3982 |
On Tue, Apr 26, 2011 at 12:29 AM, Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> wrote: > for function in actions: > results.append(function()) Can this become: results = [function() for function in actions] Chris Angelico
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2011-04-25 16:26 -0700 |
| Message-ID | <7x8vuxq5ea.fsf@ruckus.brouhaha.com> |
| In reply to | #3988 |
Chris Angelico <rosuav@gmail.com> writes: > results = [function() for function in actions] results = map(apply, actions)
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-04-26 02:38 +0000 |
| Message-ID | <4db6301d$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #4007 |
On Mon, 25 Apr 2011 16:26:37 -0700, Paul Rubin wrote: > Chris Angelico <rosuav@gmail.com> writes: >> results = [function() for function in actions] > > results = map(apply, actions) Sadly not in Python 3, where map is lazy and you need to add a call to list to make it equivalent to the list comp. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2011-04-25 23:28 +0200 |
| Message-ID | <ip4p1v$gmf$1@r03.glglgl.eu> |
| In reply to | #3982 |
Am 25.04.2011 16:29, schrieb Thomas Rachel:
> or maybe even better (taking care for closures):
>
> function = bool
> value = 'the well at the end of the world'
> ## ...
> actions.append(lambda val=value: function(val))
> ## ...
> for function in actions:
> results.append(function())
Or yet even better:
class Job(object):
def __init__(self, target, *args, **kwargs):
self.target = lambda: target(*args, **kwargs)
def __call__(self):
return self.target()
in order to do
actions.append(Job(function, val))
actions.append(Job(function, x=val))
and then (thanks, Chris...)
results = [function() for function in actions]
or maybe (additionally or alternatively)
class ActionQueue(list):
def addJob(self, target, *args, **kwargs):
self.append(lambda: target(*args, **kwargs))
def __call__(self):
if 0: # first thought
for job in self:
yield job()
else: # second thought - clean up...
while self:
job = self.pop(0)
yield job()
with
actions = ActionQueue()
actions.addJob(function, val)
actions.addJob(function, x=val)
results = list(actions()) # for collecting them and having them at once
# with generator, all call results can as well be emitted as soon as
they are available - depending what shall be done with the results
mmm... too much imagination I think... ;-)
Thomas
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-04-25 17:44 -0600 |
| Message-ID | <mailman.824.1303775109.9059.python-list@python.org> |
| In reply to | #4002 |
On Mon, Apr 25, 2011 at 3:28 PM, Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> wrote: > Am 25.04.2011 16:29, schrieb Thomas Rachel: > >> or maybe even better (taking care for closures): >> >> function = bool >> value = 'the well at the end of the world' >> ## ... >> actions.append(lambda val=value: function(val)) >> ## ... >> for function in actions: >> results.append(function()) > > Or yet even better: > > class Job(object): > def __init__(self, target, *args, **kwargs): > self.target = lambda: target(*args, **kwargs) > def __call__(self): > return self.target() > > in order to do > > actions.append(Job(function, val)) > actions.append(Job(function, x=val)) from functools import partial actions.append(partial(function, val)) actions.append(partial(function, x=val)) Cheers, Ian
[toc] | [prev] | [next] | [standalone]
| From | Mel <mwilson@the-wire.com> |
|---|---|
| Date | 2011-04-08 16:42 +0000 |
| Message-ID | <inndtd$ndo$1@dont-email.me> |
| In reply to | #2855 |
candide wrote: > About the standard function bool(), Python's official documentation > tells us the following : > > bool([x]) > Convert a value to a Boolean, using the standard truth testing procedure. > > In this context, what exactly a "value" is referring to ? > > For instance, > >>> x=42 > >>> bool(x=5) > True > >>> Cute. What's happening here is that `x=5` isn't really an expression. It's passing a value to the named parameter `x`, specified in the definition of `bool`. Try it with something else: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> bool(y=5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'y' is an invalid keyword argument for this function Mel.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-04-08 10:43 -0600 |
| Message-ID | <mailman.155.1302281042.9059.python-list@python.org> |
| In reply to | #2855 |
On Fri, Apr 8, 2011 at 10:26 AM, candide <candide@free.invalid> wrote: >>>> x=42 >>>> bool(x=5) > True >>>> > > > but _expression_ : > > x=42 > > > has no value. "x=42" is an assignment statement, not an expression. In "bool(x=5)", "x=5" is also not an expression. It's passing the expression "5" in as the parameter x, using a keyword argument.
[toc] | [prev] | [next] | [standalone]
| From | candide <candide@free.invalid> |
|---|---|
| Date | 2011-04-08 23:32 +0200 |
| Message-ID | <4d9f7efc$0$1065$426a74cc@news.free.fr> |
| In reply to | #2859 |
Le 08/04/2011 18:43, Ian Kelly a écrit : > "x=42" is an assignment statement, not an expression. Right, I was confounding with C ;) In fact, respect to this question, the documentation makes things unambiguous : ----------------- In contrast to many other languages, not all language constructs are expressions. There are also statements which cannot be used as expressions, such as print or if. Assignments are also statements, not expressions. ----------------- > In "bool(x=5)", "x=5" is also not an expression. It's passing the > expression "5" in as the parameter x, using a keyword argument. You are probably right but how do you deduce this brilliant interpretation from the wording given in the documentation ?
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-04-08 15:03 -0700 |
| Message-ID | <mailman.164.1302299590.9059.python-list@python.org> |
| In reply to | #2877 |
candide wrote: > Le 08/04/2011 18:43, Ian Kelly a écrit : >> In "bool(x=5)", "x=5" is also not an expression. It's passing the >> expression "5" in as the parameter x, using a keyword argument. >> > You are probably right but how do you deduce this brilliant > interpretation from the wording given in the documentation ? Look at your original post, which contains the excerpt from the docs that you put there: > > bool([x]) > Convert a value to a Boolean, using the standard truth testing > procedure. > As you can see, the parameter name is 'x'. ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | candide <candide@free.invalid> |
|---|---|
| Date | 2011-04-09 00:59 +0200 |
| Message-ID | <4d9f9383$0$21107$426a74cc@news.free.fr> |
| In reply to | #2879 |
Le 09/04/2011 00:03, Ethan Furman a écrit : > > bool([x]) > > Convert a value to a Boolean, using the standard truth testing > > procedure. > > > > As you can see, the parameter name is 'x'. OK, your response is clarifying my point ;) I didn't realize that in the bool([x]) syntax, identifier x refers to a "genuine" argument [I was considering x as referring to a "generic" object having a boolean value]. Nevertheless, compare with the definition the doc provides for the builtin function dir(): dir([object]) [definition omited, just observe the declaration syntax] Now, lets make a try >>> dir(object="Explicit is better than implicit") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: dir() takes no keyword arguments >>> Not very meaningful, isn't it ?
[toc] | [prev] | [next] | [standalone]
| From | Lie Ryan <lie.1296@gmail.com> |
|---|---|
| Date | 2011-04-09 15:45 +1000 |
| Message-ID | <4d9ff118$1@dnews.tpgi.com.au> |
| In reply to | #2885 |
On 04/09/11 08:59, candide wrote:
> Le 09/04/2011 00:03, Ethan Furman a écrit :
>
>> > bool([x])
>> > Convert a value to a Boolean, using the standard truth testing
>> > procedure.
>> >
>>
>> As you can see, the parameter name is 'x'.
>
>
> OK, your response is clarifying my point ;)
>
>
> I didn't realize that in the bool([x]) syntax, identifier x refers to a
> "genuine" argument [I was considering x as referring to a "generic"
> object having a boolean value].
>
>
> Nevertheless, compare with the definition the doc provides for the
> builtin function dir():
>
> dir([object])
> [definition omited, just observe the declaration syntax]
>
> Now, lets make a try
>
>>>> dir(object="Explicit is better than implicit")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: dir() takes no keyword arguments
>>>>
>
> Not very meaningful, isn't it ?
The error says it unambiguously, dir() does not take *keyword*
arguments; instead dir() takes *positional* argument:
dir("Explicit is better than implicit")
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web