Explain the encapsulated anonymous function syntax

It doesn’t work because it is being parsed as a FunctionDeclaration, and the name identifier of function declarations is mandatory. When you surround it with parentheses it is evaluated as a FunctionExpression, and function expressions can be named or not. The grammar of a FunctionDeclaration looks like this: function Identifier ( FormalParameterListopt ) { FunctionBody … Read more

What is this weird colon-member (” : “) syntax in the constructor?

Foo(int num): bar(num) This construct is called a Member Initializer List in C++. Simply said, it initializes your member bar to a value num. What is the difference between Initializing and Assignment inside a constructor? Member Initialization: Foo(int num): bar(num) {}; Member Assignment: Foo(int num) { bar = num; } There is a significant difference … Read more

Is there a standardized method to swap two variables in Python?

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. Python docs: Evaluation order That means the following for the expression a,b = b,a : The right-hand side b,a is evaluated, that is to say, a tuple of two elements is created in … Read more

How to use a dot “.” to access members of dictionary?

I’ve always kept this around in a util file. You can use it as a mixin on your own classes too. class dotdict(dict): “””dot.notation access to dictionary attributes””” __getattr__ = dict.get __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ mydict = {‘val’:’it works’} nested_dict = {‘val’:’nested works too’} mydict = dotdict(mydict) mydict.val # ‘it works’ mydict.nested = … Read more

Is C++ context-free or context-sensitive?

Below is my (current) favorite demonstration of why parsing C++ is (probably) Turing-complete, since it shows a program which is syntactically correct if and only if a given integer is prime. So I assert that C++ is neither context-free nor context-sensitive. If you allow arbitrary symbol sequences on both sides of any production, you produce … Read more

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