Python assignment destructuring

According to dis, they all get compiled to the same bytecode: >>> def f1(line): … a,b,c = line.split() … >>> def f2(line): … (a,b,c) = line.split() … >>> def f3(line): … [a,b,c] = line.split() … >>> import dis >>> dis.dis(f1) 2 0 LOAD_FAST 0 (line) 3 LOAD_ATTR 0 (split) 6 CALL_FUNCTION 0 9 UNPACK_SEQUENCE 3 … Read more

React JS Error: Invalid attempt to destructure non-iterable instance

I caused this error a few times because whenever I write a useState hook, which I would do often, I’m used to using an array to destructure like so: const [ state, setState ] = useState(); But my custom hooks usually return an object with properties: const { data, isLoading } = useMyCustomFetchApiHook(); Sometime I … Read more

How to destructure an object with a key containing a hyphen into a variable? [duplicate]

Just like you cannot declare a variable with a hyphen, you can’t destructure directly to one. You will need to rename your variable to something else in order to access it on the current scope. You can use the following destructuring syntax to do that: const x = { “accept-ranges”:”bytes”, “cache-control”:”public, max-age=0″, “content-length”:”1174″, “content-type”:”application/json”, date:”Mon, … Read more

React – defaultProps vs ES6 default params when destructuring (performances issues)

I talked to several people on Discord #reactiflux channel and actually got the answer I was looking for. There are basically three use-case with React components, and in some of them, destructuring params will impact performances so it is important to understand what’s going on under the hood. Stateless functional component const MyComponent = ({ … Read more

How can I emulate destructuring in C++?

In C++17 this is called structured bindings, which allows for the following: struct animal { std::string species; int weight; std::string sound; }; int main() { auto pluto = animal { “dog”, 23, “woof” }; auto [ species, weight, sound ] = pluto; std::cout << “species=” << species << ” weight=” << weight << ” sound=” … Read more

Destructuring in Node.JS

Update for node v6 and newer: Node v6 supports destructuring assignment without anything special needed: var [a, b] = [1, 2]; For older versions of node: You can get the list of supported harmony features by typing: node –v8-options | grep harmony node 5.x will give you: –es_staging (enable all completed harmony features) –harmony (enable … Read more

ES6 Destructuring and Module imports

import {Link} from ‘react-router’; imports a named export from react-router, i.e. something like export const Link = 42; import Router from ‘react-router’; const {Link} = Router; pulls out the property Link from the default export, assuming it is an object, e.g. export default { Link: 42 }; (the default export is actually nothing but a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)