Python Forum

Full Version: Type float doesn't have expected attribute '__div__'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm playing with some formulas and I ran across this warning
 Type float doesn't have expected attribute '__div__' with the  "resist_2" in this line:

volt_div(volt_in, resist_1, resist_2)
Here is the full function:
def volt_div(v_in, res_1, res_2):
    # resistor_2 = res_2    # Needed to make Python's __div__ happy
    v_out = v_in * (res_2 / (res_1 + res_2))

    print("Output of voltage divider = {:.4f} VDC".format(v_out))
    return

volt_in = 10.0
resist_1 = 10.0
resist_2 = 10.0

volt_div(volt_in, resist_1, resist_2)
It doesn't seem to affect the outcome, but I am curious as to what it means.
Is that coming from Pycharm? I just tried it on OS X, Python 3.5.2 and Python 2.7.12 without seeing that message.
Yes, sorry, it's a warning in Pycharm.  The only info it gives is it's related to "dynamic dispatch and duck typing...".  What I find confusing  is if I rename (declare?) the variable within the function (renaming "res_2" to "resistor_2)" the warning goes away.  That, however seems a rather clunky way of fixing a problem,
python 3 uses __truediv__ instead of __div__ (and __floordiv__)

from docs:
Quote:operator.truediv(a, b)
operator.__truediv__(a, b)

    Return a / b where 2/3 is .66 rather than 0. This is also known as “true” division.
So it's simply saying it's not backward compatible?  It's rather late and my brain is not as sharp as it should be (if it ever is) but why would it make a difference if the variable is defined within the function (which is apparently o.k.) as opposed to being passed to the function (which is not o.k.)
I'm fairly certain it's a Pycharm issue similar to https://intellij-support.jetbrains.com/h...arm-2017-1
Thanks for going to the trouble of finding that post, I seriously doubt I would have found it.  I was certain it was a problem with how I had defined the function, it never crossed my mind it might be a problem with Pycharm.

Thanks again.
Quote:but why would it make a difference if the variable is defined within the function
It shouldn't, micsydel has to be right

There are other things in PyCharm that I haven't liked lately,
auto indent doesn't work properly with cut and paste for one and it's quite annoying.
The function is quite simple in order to cause this.