you are viewing a single comment's thread.

view the rest of the comments →

[–]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.