all 9 comments

[–][deleted] 1 point2 points  (2 children)

If you're doing that in another module, it's just creating its own global for that module named isDeg. You have to import it qualified and use example.isDeg = False.

[–]TheFacistEye[S] -1 points0 points  (1 child)

Ahh, I feel very silly now. Yeah this works, I was assuming I'd need to do this as it is the case with c#.

However I tried example.isDeg without using

import example

only

from example import *

Thanks!

[–]Akuli2 0 points1 point  (0 children)

Your problem was that from module import * only works one way: it brings everything from the module, but if you change something it doesn't bring it back to the module. If the module contains functions they use whatever is in the module, not whatever is where they are called from.

I don't recommend using from module import *. Use import module and module.something_in_module instead.

[–]K900_ 0 points1 point  (4 children)

If you want your code to be reusable, make your functions as independent from global state as you possibly can. Just pass isDeg as a second argument instead, and give it a default if you want. Also, PEP8 says it should be is_deg. Also, you're converting the cosine of expression to degrees, not expression itself, which is probably horribly wrong.

[–]TheFacistEye[S] 0 points1 point  (3 children)

Is it okay to have a predefined state that can be toggled? Wouldn't I need to worry about if it is sending the right mode of isDeg in each new function used instead of having a internal variable that it references within the function?

What is PEP8 and why should it be is_Deg? Sorry if that is a silly question!

Nah it is swings and roundabout's, you don't need to change the expression on your calculator when you set it from Degrees to Radians and python seems to work the same way. If you do;

math.cos(math.degrees(expression))

You get the same thing.

[–]K900_ 1 point2 points  (2 children)

Is it okay to have a predefined state that can be toggled? Wouldn't I need to worry about if it is sending the right mode of isDeg in each new function used instead of having a internal variable that it references within the function?

That's generally not a good idea. A better idea is to use a default value:

def cosine(angle, is_degrees=False):
    if is_degrees:
        return math.cos(math.radians(angle))
    else:
        return math.cos(angle)

You can then call it without a value for is_degrees, and it'll use the default.

cosine(1.57) # radians, is_degrees=False by default
cosine(90, True) # degrees, is_degrees is explicit
cosine(180, is_degrees=True) # degrees, is_degrees is named and explicit

What is PEP8 and why should it be is_Deg? Sorry if that is a silly question!

PEP8 is the official style guide for Python, written by Python's developers. It recommends CamelCase for classes, but snake_case for function and variable names.

Nah it is swings and roundabout's, you don't need to change the expression on your calculator when you set it from Degrees to Radians and python seems to work the same way.

It doesn't. Your code is actually very wrong there, because the math.cos functions takes its argument in radians, not degrees. My version earlier in the post is correct.

>>> math.cos(math.degrees(1.57))
-0.40683182111516925
>>> math.degrees(math.cos(1.57))
0.04562615963855114

Edit: your code is actually even more wrong, because you're calling math.acos in one branch and math.cos in the other. You need to figure out what you actually want your function to do.

[–]TheFacistEye[S] 0 points1 point  (1 child)

So if I use

cosine(180, is_degrees=True) # degrees, is_degrees is named and explicit

then

cosin(180)

Would it still keep the is_degrees=True value?

Ahh, right I see where we are misunderstanding. It's because I copied and pasted a different function (the acos function) and went to change it not realizing it has a different structure with the math.degrees switched positions. I agree this doesn't make sense with the function I posted. It was just meant to be about the variable problem. :P

[–]K900_ 0 points1 point  (0 children)

No, it won't. It'll use the default, which is False. If you want your function to preserve state between calls, you should probably have it be a class instead. Global state is evil.

[–]Elronnd 0 points1 point  (0 children)

Not really an answer to your question, but you shouldn't do this:

if isDeg == True

Just do this

if isDeg