473,594 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1568
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...@pixelware inc.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...@pixelware inc.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.serversdire ct.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_endt ag(i)
File "/usr/local/lib/python2.5/sgmllib.py", line 315, in parse_endtag
self.finish_end tag(tag)
File "/usr/local/lib/python2.5/sgmllib.py", line 353, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeEr ror: '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...@chiar k.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
4019
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 the numbers are close, they add up; if they're real far apart, it gives the bigest only, and sometimes it gives the biggest +1 or +2. could someome please confirm the effects.
8
2064
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 now Defect Report 484 on the standard library. I'm beginning to wonder if I've just misunderstood the purpose of iterator_traits::value_type. I constructed an input iterator where *i returns an int, but I set iterator_traits::value_type to bool....
19
10154
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. I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350 bit floats. Yes, 350. We have a lot of fast machines now. Why not use them? The extra-precision is useful in some algorithms.
3
2940
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 delegates?
9
2191
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 using the overridden __eq__, while the __lt__ method is never called. Someone can explain me why?
6
1790
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 letters, source code, whatever. In particular, does anyone have much experience with the Python interface to Terence Parr's StringTemplate (http://www.stringtemplate.org/)? Reading the website, I'm attracted by
17
1463
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
2918
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. Download for free at http://www.thinkanddone.com/prog/java/parser.html If the above link does not work try http://www.britishcomputercolleges.com/prog/java/parser2.html
4
2008
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 help me. Lets say X is my vector, which contains 10 elements. I want to assign elements indexed 2 to 5 to another vector Y, so I want to do it like Y = X(2,5). So what I do is i overload the () operators, as marked in (a) below. This creates a new...
0
7946
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
7877
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
8253
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
8240
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
6661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5411
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3867
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...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.