What does t /= d mean? Python and getting errors

2024/9/19 9:53:33
// t: current time, b: begInnIng value, c: change In value, d: durationdef: 'easeOutQuad',
swing: function (x, t, b, c, d) {//alert(jQuery.easing.default);return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
},
easeInQuad: function (x, t, b, c, d) {return c*(t/=d)*t + b;
},
easeOutQuad: function (x, t, b, c, d) {return -c *(t/=d)*(t-2) + b;
},

I am trying to convert Robert Penner's Easing Functions into python and getting stuck! Any help or any one else done this before?

https://github.com/danro/jquery-easing/blob/master/jquery.easing.js

Answer

In both JavaScript and Python, /= is an "augmented assignment" operator, with pretty much the same meaning.

In JS:

var i = 10;
i /= 2;

… is equivalent to:

var i = 10;
i = i / 2;

And, in Python:

i = 10
i /= 2

… is likewise equivalent (not quite exactly the same, but close enough for numbers) to:

i = 10
i = i / 2

However, there is one very big difference.

In JavaScript, assignment is an expression—it has a value, and that value is the value being assigned to the variable. So:

var i = 10;
var j = i /= 2;

… is roughly equivalent to:

var i = 10;
i /= 2;
var j = i;

In Python, assignment is a statement. It has no value, and cannot be used in an expression. So:

i = 10
j = i /= 2

… raises a SyntaxError.


Porting code that uses assignment (augmented or otherwise) in the middle of an expression generally requires breaking that expression up into multiple lines and/or finding a way to rewrite the expression to not require any assignments. (But often, that's not a bad thing, because the original expressions weren't very readable anyway…)

For example, assuming JS evaluates operands from left to right (which I'm not sure is guaranteed?):

def easeInQuad(x, t, b, c, d):t /= dreturn c*t*t+b

More generally, you do this:

old_t = t
t /= d

And then you replace any instances of t before that t/=d with old_t, and leave all instances from t/=d and later alone. Fortunately, in this case, there are no previous instances, so we don't need that old_t stuff.

And if you think about it, you can easily get the same effect without ever changing t, in one line, much more readably, in any of the following ways:

return c * (t/d) * (t/d) + b
return c * (t/d)**2 + b
return c * t*t / d*d + b

Someone who thinks in C will immediately complain that these are all "too slow". After all, the first does an extra division, the second does an exponentiation instead of a multiplication, and the third does two multiplications instead of one. Horrors!

Of course you can always use a temporary variable:

t_over_d = t/d
return c * t_over_d * t_over_d + b

… but again, to a C programmer, that implies that you're using up a valuable register. Sure, every compiler written after, say, 1985 will detect that t is dead as soon as t_over_d appears and reuse the same register, but why not force it to reuse the register if we can, especially if it saves a few keystrokes too?

In JS or Python, the cost of a multiplication is such a tiny fraction of the cost of calling a function, and interpreting the bytecode, and so on that you'd never even notice it. Meanwhile, the cost of rebinding a local variable (especially in a V8-style or PyPy-style JIT interpreter) might be much, much higher than the cost of passing around an unnamed temporary result.

So, this is a paradigm case of misguided "optimization" making code much harder to understand, while probably slowing it down instead of speeding it up, and in an area that cannot possibly be a bottleneck worth optimizing anyway.


Since gnibbler brought up the question of whether JavaScript actually does guarantee this evaluation order…

First, JavaScript is defined as, effectively, "what Firefox does" (and "what Spidermonkey does", but that ought to be the same thing—and, if it isn't, then JavaScript does 2 things, so that's twice as good, right?). But ECMAScript is defined by standards, and it's those standards that every JS implementation (despite the name) pays lip service to, and we can pretend that ECMAScript 5.1 is the standard that all implementations conform to (which is true so long as "all implementations" means "Opera"). You can find it here.

So, in ES 5.1: 11.5 Multiplicative Operators guarantees that the result of (t/=d) will be evaluated before t, and 11.13.2 Compound Assignment guarantees that evaluating t/=d will set the value of t before it finishes. (You have to read up on what "evaluate" means and what GetValue an SetValue mean, but I'm pretty sure this really is guaranteed.)

https://en.xdnf.cn/q/72762.html

Related Q&A

Python File Read + Write

I am working on porting over a database from a custom MSSQL CMS to MYSQL - Wordpress. I am using Python to read a txt file with \t delineated columns and one row per line.I am trying to write a Python …

Uploading a file via pyCurl

I am trying to convert the following curl code into pycurl. I do not want to use requests. I need to use pycurl because requests is not fully working in my old python version.curl -X POST -H "Acce…

Speed up computation for Distance Transform on Image in Python

I would like to find the find the distance transform of a binary image in the fastest way possible without using the scipy package distance_trnsform_edt(). The image is 256 by 256. The reason I dont wa…

Prevent Kivy leaving debug messages

I have a simple a Kivy interface that also uses the terminal. Example code:import kivykivy.require(1.0.6)from kivy.app import App from kivy.uix.label import Labelclass MyApp(App):def build(self):return…

django.db.utils.InternalError: (1050, Table django_content_type already exists)

django.db.utils.InternalError: (1050, "Table django_content_type already exists")I just copied a project from my friend, when I run makemirations it runs properly. But for - python3 manage.py…

Use SQLites backup API from Python/SQLAlchemy

Im using an SQLite database from python (with SQLAlchemy). For performance reasons, Id like to populate an in-memory database in the application, and then back up that database to disk.SQLite has a bac…

ABC for String?

I recently discovered abstract base classes (ABCs) in collections and like their clear, systematic approach and Mixins. Now I also want to create customs strings (*), but I couldnt find an ABC for stri…

Get the most relevant word (spell check) from enchant suggest() in Python

I want to get the most relevant word from enchant suggest(). Is there any better way to do that. I feel my function is not efficient when it comes to checking large set of words in the range of 100k or…

How do I get python-markdown to additionally urlify links when formatting plain text?

Markdown is a great tool for formatting plain text into pretty html, but it doesnt turn plain-text links into URLs automatically. Like this one:http://www.google.com/How do I get markdown to add tags …

Best way to read aws credentials file

In my python code I need to extract AWS credentials AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID which are stored in the plain text file as described here: https://docs.aws.amazon.com/sdkref/latest/guid…