Purpose of operator package

E

Eric Anderson

I mainly work in other languages (mostly Ruby lately) but my text
editor (Scribes) is python. With python being everywhere for dynamic
scripting I thought I would read the source to learn the language
better (I've gone through some basic tutorials but I always prefer to
learn from real source).

So right from the start I see the following:

from operator import truth
if truth(argv):
# blah blah blah

It is obvious they are testing to see if any command line arguments.
But curious for why the function is needed. So I look up the operator
package and fine it provides functions that are equivalent to the
native operators. So my question is why would someone right the above
instead of just

if argv:
# blah blah blah

Seems like unnecessary code but obviously I know nothing about Python.

Thanks for any pointers!
 
C

Christian Heimes

Eric said:
Seems like unnecessary code but obviously I know nothing about Python.

Correct, the truth example isn't a good example. "if argv" is better.
But you can write interesting things with the operator module. For example
.... return reduce(operator.mul, range(1, x+1))
....24

In general one doesn't use the operator module.

Christian
 
C

Carl Banks

I mainly work in other languages (mostly Ruby lately) but my text
editor (Scribes) is python. With python being everywhere for dynamic
scripting I thought I would read the source to learn the language
better (I've gone through some basic tutorials but I always prefer to
learn from real source).

There is one significant drawback of that....

So right from the start I see the following:

from operator import truth
if truth(argv):
# blah blah blah

It is obvious they are testing to see if any command line arguments.
But curious for why the function is needed.

It isn't. The above is terrible code.

1. You don't even need operator.truth; the built-in bool performs that
job. However, this code could have been written before the advent of
bool, so we'll give it a temporary pass.
2. bool in unnecessary in this context.
3. Lest someone claim that truth serves to document that you are
asking for the boolean value of argv (i.e., whether it's not empty),
it's redundnant since the if statement implies that.

So I look up the operator
package and fine it provides functions that are equivalent to the
native operators. So my question is why would someone right the above
instead of just

if argv:
# blah blah blah

Seems like unnecessary code but obviously I know nothing about Python.

You know more than the person who wrote the code above.

The purpose of the operator module is to provide functional
representations of Python operators (and a few non-operators) for
functional programming. They could, for example, be useful as the
arguments of map and reduce. Few beginners use functional programming
so you might not want to worry about it now, though if you've been
using Ruby you might have done it before.

operator.truth was one of the few things in that module that was
useful for things other than functional programming, since there is no
truth operator in Python. Now that the language has bool it's no
longer needed.


Carl Banks
 
C

Carl Banks

There is one significant drawback of that....




It isn't. The above is terrible code.

1. You don't even need operator.truth; the built-in bool performs that
job. However, this code could have been written before the advent of
bool, so we'll give it a temporary pass.
2. bool in unnecessary in this context.
3. Lest someone claim that truth serves to document that you are
asking for the boolean value of argv (i.e., whether it's not empty),
it's redundnant since the if statement implies that.

4. sys.argv is never false. I presume that that the first item of
argv was removed, or argv is a copy of sys.argv without the first
element. Either way is bad style IMO. Leave sys.argv alone, and if
you copy it without the first element, copy it to a differently-named
variable.



Carl Banks
 
J

John Nagle

Here's another example of the annoying "attributes must be ASCII
but sgmllib doesn't check" problem.

Run "http://www.serversdirect.com" through BeautifulSoup, and watch it
blow up at this bogus HTML:

<LI>Support Multi-Core Intel® Xeon® processor 3200/3000 sequence
</LISUPPORT sequence 32003000 processor xeon® intel® multi-core>

The parser uses the ® symbol as part of an attribute name:

SGMLParser.feed(self, markup or "")
File "/usr/local/lib/python2.5/sgmllib.py", line 99, in feed
self.goahead(0)
File "/usr/local/lib/python2.5/sgmllib.py", line 138, in goahead
k = self.parse_endtag(i)
File "/usr/local/lib/python2.5/sgmllib.py", line 315, in parse_endtag
self.finish_endtag(tag)
File "/usr/local/lib/python2.5/sgmllib.py", line 353, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 46:
ordinal not in range(128)

And we're downhill from there. Probably worth fixing, since it's one of the
few real-world HTML bugs that totally blows up BeautifulSoup.

John Nagle
SiteTruth
 
M

Matthew Woodcraft

I V said:
I hadn't heard of operator.truth before. Does it do anything different
from bool(x) ?

Not really. It was occasionally useful before the bool type existed;
now it's just a leftover.

-M-
 
C

castironpi

Not really. It was occasionally useful before the bool type existed;
now it's just a leftover.

-M-

Now as for ints, I could see that going in to 8-by-8s and crosses.
Anyone for?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top