473,756 Members | 3,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unary plus operator and __pos__

I was checking the Prolog recipe in the Cookbook:

http://aspn.activestate.com/ASPN/Coo.../Recipe/303057

It's a clever implementation that explores some aspects of Python that
I wasn't aware of. One of them is the unary plus operator, that calls
the __pos__ method. It's something that can be highly useful in my own
experiments with the use of classes as a tool for generic declarative
descriptions of objects -- UI forms, reports, webpages, etc.

Now I'm curious about the operator itself. Why is the unary plus
operator associated with the __pos__ magic method? I' can't see a
relation here, and I could not find much info (although I haven't
really looked very hard :-)

Anyone knows why is it so?

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmai l.com
mail: ca********@yaho o.com
Jul 18 '05 #1
3 3905
Carlos Ribeiro wrote:
Now I'm curious about the operator itself. Why is the unary plus
operator associated with the __pos__ magic method? I' can't see a
relation here, and I could not find much info (although I haven't
really looked very hard :-)

Anyone knows why is it so?


Unary plus is __pos__ for positive, unary minus is __neg__ for negative.

The unary plus operator, I'm sure, is a holdover from C. Since the
unary plus operator is a no-op for normal numeric values, I would be
hesitant to use it to mean something else; I'm a firm believer in
careful use of operator overloading, prefering to do so hopefully only
when the meaning is apparent from conventional operator usage. Since
unary + is a no-op, I don't think it would be a great idea to use it to
mean something vastly different, like "add a fact to the database." But
to each his own.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ But the system has no wisdom / The Devil split us in pairs
-- Public Enemy
Jul 18 '05 #2
Erik Max Francis wrote:
Carlos Ribeiro wrote:
Now I'm curious about the operator itself. Why is the unary plus
operator associated with the __pos__ magic method? I' can't see a
relation here, and I could not find much info (although I haven't
really looked very hard :-)

Anyone knows why is it so?


Unary plus is __pos__ for positive, unary minus is __neg__ for negative.

The unary plus operator, I'm sure, is a holdover from C. Since the
unary plus operator is a no-op for normal numeric values, I would be
hesitant to use it to mean something else;


The Decimal class does:
n = Decimal((0, (0, ), 'N'))
n Decimal("sNaN") +n

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.4/decimal.py", line 863, in __pos__
ans = self._check_nan s(context=conte xt)
File "/usr/local/lib/python2.4/decimal.py", line 619, in _check_nans
1, self)
File "/usr/local/lib/python2.4/decimal.py", line 2243, in _raise_error
raise error, explanation
decimal.Invalid Operation: sNaN

yours,
Gerrit.

--
Weather in Twenthe, Netherlands 28/09 08:55:
15.0°C mist overcast wind 4.0 m/s SW (57 m above NAP)
--
In the councils of government, we must guard against the acquisition of
unwarranted influence, whether sought or unsought, by the
military-industrial complex. The potential for the disastrous rise of
misplaced power exists and will persist.
-Dwight David Eisenhower, January 17, 1961
Jul 18 '05 #3
Gerrit wrote:
Erik Max Francis wrote:
Unary plus is __pos__ for positive, unary minus is __neg__ for
negative.

The unary plus operator, I'm sure, is a holdover from C. Since the
unary plus operator is a no-op for normal numeric values, I would be
hesitant to use it to mean something else;


The Decimal class does:
n = Decimal((0, (0, ), 'N'))
n Decimal("sNaN") +n

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.4/decimal.py", line 863, in __pos__
ans = self._check_nan s(context=conte xt)
File "/usr/local/lib/python2.4/decimal.py", line 619, in _check_nans
1, self)
File "/usr/local/lib/python2.4/decimal.py", line 2243, in _raise_error
raise error, explanation
decimal.Invalid Operation: sNaN


I don't see how that's at all a useful feature. Besides, one add-on
module as a counterexample doesn't exactly invalidate the point, which
was a personal opinion on a design goal, anyway.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ History is a set of lies agreed upon.
-- Napoleon Bonaparte
Jul 18 '05 #4

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

Similar topics

8
3824
by: Elementary Penguin | last post by:
Is there a left add unary operator ( or do I have to overload one ) ? For example: string s1 = "A"; s1 += "B"; Console.WriteLine( s1 )
5
2640
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
17
3501
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means that we can write, (but not allowed) 1. 2! 2. !2
2
2262
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program with output.
2
2371
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
13
5095
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even touched the standards a year ago, though I graduated in embedded SW development... Anyway, to the problem at hand: I've stumbled upon the following construct at work recently, and cannot really make up my mind about the standard's take on the matter.
28
5079
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be the ++, the compiler says: "Error: invalid l-value in increment". int i = 10; ~!*&i++;
2
2170
by: Michael A. Covington | last post by:
Other than to create confusion, does the unary plus operator serve any purpose at all? All C# numeric types have a unary plus. The syntax is something like: x = +y; where +y is the same value as y, whether positive or negative.
16
4717
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as an unary operator. Why is that? Is it just a syntax cotegory? Thanks.
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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
8713
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...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
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
5142
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...
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.