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

Purpose of operator package

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!
Jun 27 '08 #1
7 1559
Eric Anderson schrieb:
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
>>import operator
def fac(x):
.... return reduce(operator.mul, range(1, x+1))
....
>>fac(2)
2
>>fac(3)
6
>>fac(4)
24

In general one doesn't use the operator module.

Christian

Jun 27 '08 #2
I V
On Wed, 14 May 2008 00:38:44 +0200, Christian Heimes wrote:
Eric Anderson schrieb:
>Seems like unnecessary code but obviously I know nothing about Python.

Correct, the truth example isn't a good example. "if argv" is better.
I hadn't heard of operator.truth before. Does it do anything different
from bool(x) ?
Jun 27 '08 #3
On May 13, 6:09 pm, Eric Anderson <e...@pixelwareinc.comwrote:
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
Jun 27 '08 #4
On May 13, 7:46 pm, Carl Banks <pavlovevide...@gmail.comwrote:
On May 13, 6:09 pm, Eric Anderson <e...@pixelwareinc.comwrote:
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.
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
Jun 27 '08 #5
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
Jun 27 '08 #6
I V <iv*****@gmail.comwrote:
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-
Jun 27 '08 #7
On May 14, 11:58*am, Matthew Woodcraft
<matth...@chiark.greenend.org.ukwrote:
I V *<ivle...@gmail.comwrote:
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-
Now as for ints, I could see that going in to 8-by-8s and crosses.
Anyone for?
Jun 27 '08 #8

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

Similar topics

7
by: Dan. | last post by:
hi, i'm sorry to bother you; but i was wondering what the function of the OR operator was in terms of numbers: num0 = num1 or num2 i wrote me a little thing to try and see the effects and if...
8
by: chris | last post by:
I tried to post this to comp.std.c++ some time ago, but for some reason I aren't getting any automatic confirmation. I thought I would therefore post it here. Some time ago I submitted what is...
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
3
by: Bob | last post by:
C# newbie here.... studying about delegates and now events. I just don't get the purpose of declaring events using the event keyword. What does this do for me that can't be done using only...
9
by: Emanuele Aina | last post by:
I have some code which does a lot of "in" on lists containing objects with no __eq__ defined. It all goes fast until I add the __lt__() method: then I have a slowdown comparable to the one I get...
6
by: John Machin | last post by:
Hi, In general, I'm mainly interested in a template engine for dynamic web pages but would like a general purpose one to avoid learning yet another package for generating e-mail messages, form...
17
by: Alexander Eisenhuth | last post by:
Hello, is there a assignement operator, that i can overwrite? class MyInt: def __init__(self, val): assert(isinstance(val, int)) self._val = val a = MyInt(10)
29
by: Dexter | last post by:
This Java based utility may be invoked from Java code to parse mathematical expressions. It is useful for programmers developing calculators, graphing utilities or other math related programs. ...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.