tags:

views:

210

answers:

4
import decimal                               # Decimals
a=decimal.getcontext().copy()
print a

thanks.

what is the useful of a.

+3  A: 

a is the name of a variable, you could just as easily have put

import decimal                               # Decimals
print decimal.getcontext().copy()

To remove the reference to a

Richo
+1  A: 

I think the answer is that in this particular case, a serves no purpose

However in the larger scheme of things, if you need to use the value of

a=decimal.getcontext().copy()

again, then you don't want to recompute it, because either the function may change or it may be an expensive operation.

piggles
+7  A: 

I am answering assuming a novice.

"a" is a variable. Variable is a logical unit that keeps a value/string etc on computer's memory while executing a program. As an example if you are going to add 1 and 2 and get the answer, you should create a "variable" in memory say "a" and assign the value of 1 + 2 for "a"

In this code you have imported a library called decimal. It contains set of operations or methods. Then you perform some operations on it and assign the output to a variable called "a". Then you are trying to print it to console.

N.B : You should not use variable names like a,b. It is not a good practice. You should use a meaningful word.

Chathuranga Chandrasekara
+2  A: 

While I agree with many of the other comments here I figured I'd throw a bone.

The decimal module is a utility for doing arithmetic on numbers of arbitrary precision. This is used in some scientific contexts to do calculations on things that suffer from round-off errors. The classical example would be calculting the 9999th digit of pi. You just can't do that with floats.

The decimal library uses the concept of a "context" to decide how much precision you really want. Since the more precision you want the slower the calculations will run. The decimal.getcontext() part returns the default context. But through a crazy "gotcha" (that I've actually fallen prey to) this is actually just a pointer. So if you were to change the context with a decimal.setcontext('foo') you would actually change a as well. Handily the module has a .copy() method.

The original programmer probably did this to save a copy of the context for later use. Perhaps s/he wanted to perform a part of the computation at a lower precision to save time and then wanted to perform the difficult part at a higher precision.

Although in python > 2.5 this is best done with a context manager like 'with'

from decimal import localcontext

with localcontext() as ctx:
    ctx.prec -= 5   # Perform a low precision calculation
    s = calculate_something()
s = +s  # Round the final result back to the default precision

But without the rest of the relevant code my guess is no better than yours.

While I have worked with the decimal module before all of this is easily found in the python docs here. Try to go there and poke around first for questions like this ... if you can figure it out yourself you'll remember it longer. And you'll get that warm feeling in your head that the rest of us feel when we answer questions.

JudoWill
@zjm1126 if you have a 100% accept rate then at least your paying attention to the responses that come back ;)
JudoWill
@JudoWill, I think the `code` part of your answer was *most* impressive. Think about it.
pavium
@pavium, I can't tell if your being facetious or serious.
JudoWill
@JudoWill: the basic problem here is that @zjm1126 is reading little if anything we write, and thus as @pavium mentions above not at all learning how to help himself. We can continue to donate code snippets, but we are wasting the OP's time and ours. He really does need to learn more English before he can gain anything from prose responses.
Adam Bernier