syntax non-error

This is the place for queries that don't fit in any of the other categories.

syntax non-error

Postby KevinD » Sun Feb 17, 2013 2:59 am

My turn to ask a question. I have some code that, in simple terms looks something like this:

Code: Select all
if <condition>:
    ct +=1
    <do some other stuff>
However, when I was running it, I discovered that although my end results were right (and that's all that mattered), the value of "ct" was never incremented. It was only being displayed as a reference, so I didn't really care. When I finally had the time to look at it more closely, I discovered that thanks to my wonderful typing skills, my code was actually written as "ct ++1" instead of "ct +=1". I never did get a syntax or other error, but a quick search didn't reveal anything about what I was doing with the "ct++1" statement. Can someone clarify what that syntax actually does, if anything?
Quanto lignum posset materiae materietur marmota Chuck si materiam possit materiari foedans, penitus lignum?
KevinD
 
Posts: 30
Joined: Fri Feb 08, 2013 3:15 am

Re: syntax non-error

Postby metulburr » Sun Feb 17, 2013 3:07 am

Code: Select all
1 + (positive)1
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1416
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: syntax non-error

Postby casevh » Sun Feb 17, 2013 4:06 am

I see that metulburr already posted the simple answer while I was composing my reply but I have reputation (especially at work) of complicated answers, so I'll still post my answer. ;)

Numeric types support a __neg__ method that returns a number with sign changed. Numeric methods also support a __pos__ method which (usually) returns the number unchanged. When multiple + or - symbols are present between two objects, the first symbol becomes addition or subtraction. The remaining symbols are applied directly to the right hand object by calling __neg__ or __pos__ as appropriate.

The += operator calls the __iadd__ method. Glossing over a few details, your intended code would be equivalent to:

Code: Select all
ct.__iadd__(1)


The code you actually entered is parsed as:

Code: Select all
ct.__add__((1).__pos__())


An addition is done but the result is never saved.

Can the + operator ever return a different number? Surprisingly, yes. For numeric types that have parameters that can be specified (say the precision of Decimal numbers), the + operator returns a number that is guaranteed to fit into the current precision. For example, if you have a value calculated to 50 digits of precision, and then you change the current precision to 40 digits, the + operator will return a number with 40 digits of precision.

Code: Select all
>>> import decimal
>>> ctx=decimal.getcontext()
>>> ctx.prec=50
>>> decimal.setcontext(ctx)
>>> a=decimal.Decimal(1)/7
>>> a
Decimal('0.14285714285714285714285714285714285714285714285714')
>>> +a
Decimal('0.14285714285714285714285714285714285714285714285714')
>>> ctx.prec=40
>>> decimal.setcontext(ctx)
>>> a
Decimal('0.14285714285714285714285714285714285714285714285714')
>>> +a
Decimal('0.1428571428571428571428571428571428571429')
>>>
casevh
 
Posts: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: syntax non-error

Postby micseydel » Sun Feb 17, 2013 4:23 am

Thanks casevh! That was neat.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1267
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: syntax non-error

Postby KevinD » Tue Feb 19, 2013 3:08 am

So, in really simple terms, I was incrementing "ct" but not saving the result, so the value always changed from 0 to 1. And since I never saved it anywhere, when I printed it after the end of my loop, it showed 0, which is correct in terms of what I was asking for, but not what I was expecting.

Got it. Thanks for the help.
Quanto lignum posset materiae materietur marmota Chuck si materiam possit materiari foedans, penitus lignum?
KevinD
 
Posts: 30
Joined: Fri Feb 08, 2013 3:15 am


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 6 guests