Initializing variables side by side - python-3.x

i=3,b=5
Why can't I run the above line in python? It says can't assign to literals error. It is not an error in Java or C++.

The big-picture answer to "why not?" is that the designer of the language was not fond of that syntax and preferred that programmers write
i, b = 3, 5
The technical reason why this is not allowed, and the explanation for your error message, can be found by reading the official grammar.
You can also experiment to see what the meaning of your phrase would be by inspecting the Python AST. Run this:
import ast
print(ast.dump(ast.parse("i=3,b")))
You get
Module(body=[Assign(targets=[Name(id='i', ctx=Store())], value=Tuple(elts=[Num(n=3), Name(id='b', ctx=Load())], ctx=Load()))])
So just writing i=3,b is an assignment of the tuple (3,b) to the variable i. Now you can write
i = b = 3
which assigns 3 to both i and b. But if you wrote
i = 3, b = 5
Then you would be trying to assign 5 to the tuple (3,b) which is not allowed.
This is the way Python is. The designer of Python wanted it to be that way. And he is the boss.

Related

What to do when a code review tool declares unmatched types?

I am working on developing a large-scale Python (backend) project. I was working with a firm that does extensive testing, and they built the frontend and test tools. Before every deploy, all the tools (like linters) are run regularly.
I had put down the code for a while, and now it fails many tests. Some of these are deprecation warnings for features or syntax soon to be deprecated, and they note they started classifying those as warnings (to later become errors) starting January 1, 2020, so I know they make dynamic changes in the tools themselves.
My problem is a bunch of code that used to pass no longer does. And the error is always the same: if I have a line that looks like so, I get an error that says something along the lines of "error: may not use operator '-' with incompatible types; a and b are of types numpy.array and NoneType":
x = a - b
This gets fixed by making the code super-messy with this sort of fix:
x = a.astype(float) - b.astype(float)
It's even worse because in the actual code there are 3 variables, all doing addition and subtraction with a 'c' that is an integer array kicking around along with the two numpy arrays. But then the code goes from:
x = a - b - c
to:
x = a.astype(float) - b.astype(float) - c.astype(float)
And this won't work since int's don't have an astype method. The error looks like this now:
File "/home/engine.py", line 165, in Foo
lower_array[t].astype(float)) / num_values.astype(float)
AttributeError: 'NoneType' object has no attribute 'astype'
Thus, I end up with:
x = a.astype(float) - b.astype(float) - float(c)
This is all extraordinarily cumbersome and nasty casting that is required, and makes the code impossible to read.
The odd thing to me is that all three arrays were instantiated as numpy arrays, i.e.,:
a=numpy.array(_a)
b=numpy.array(_b)
c=numpy.array(_c)
When I ask the code to put output to stdout the type of all three vars, they all say . Yet, the next line of code blows up and dumps, saying "Attribute error: 'NoneType' object has no attribute 'astype'"
I can't fathom how a static code analyzer determines the types - other than as numpy.ndarray type - since Python uses duck-typing. Thus, the type could change dynamically. But that's not the case here; all three vars are identified as numpy.ndarray type, but "z = a - b - c" fails.
Anyone understand what's going on here?
After much work, the answer is to ignore the linter. Readable code is the object, not code that satisfies a linter.

Basic dialect, what language is this?

I'm using Concerto post processing app from AVL and I'm having hard time with scripting language we use here. It looks like MS visual basic but not that much and I want to know more about what version of Basic is this, so that I can find more documentation on the web.
When I try code from MS Visual Basic documentation site, like generating int array and puting some elements to initialize it (as you can see below), I got syntax error on Concerto Scripting editor
' Declare a single-dimension array and set its 4 values.
Dim numbers = New Integer() {1, 2, 4, 8}
This is from Concerto's own documentation and I cannot find how to create a simple array but Dataset istead, which is something similar I beleive
NewDSArray
This function generates a new array.
Syntax:
A = NewDSArray([Rows], [Columns])
Parameters:
Rows (optional, 1 = default): Numeric initial number of lines
Columns (optional, 0 = default): Numeric initial number of columns
Function:
A is now a new array with which the Dataset Array class commands can be used.
thisDSMatrix=newdsarray(1,2)
FirstCol={1,2,3}
FirstCol.name="FirstCol" //.name will pass the name into the matrix
thisDSMatrix.PutCell(FirstCol,1,1)
SecondCol={4,5,6}
SecondCol.name="SecondCol"
thisDSMatrix.PutCell(SecondCol,1,2)
Mcols=thisDSMatrix.ColCount //returns 2
Mrows=thisDSMatrix.RowCount //returns 1
thisDSMatrix.AddColumn("MyNewCol") //a column is added and thisDSMatrix.ColCount will now show 3
thisDSMatrix.PutCell({7,8,9},1,thisDSMatrix.ColCount)
return thisDSMatrix
I appreciate if you can help me to spot exact version of Basic Concerto uses. Thank you.
We had basically the same issue and because I didn't want to learn a proprietary language I dicided to go with Python. AVL Concerto provides from version V5.x onwards a Python-API. Despite that the API is not perfect from my point of view, this might be an alternative to the Concerto scripting language. And learning Python will thertenly the better solution.

Python - Reading File Then Adding Value to Sum?

I've been looking all around the web for this answer and can't find it.
I'm semi new to python so hoping i get an answer here,
so basically what i want to do is, access a text file "number.txt"
that has a 10 as a line, and do a sum within the python code.
Here is what i got so far:
with open('number.txt', 'r') as sum:
num = sum.readline()
clean = num.rstrip('\n')
#number.txt file only contains 1 line and is a 10
increase = "5"
adding = clean + increase
print(adding)
it doesn't do the sum, instead i get the 5 added after the 10
so instead of getting 15 i get 105.
can anyone help?
Welcome to programming. Keep aware of the concepts of data types, I suggest doing some reading on that.
The + operator behaves differently between strings, arrays, and integers.
I would give you the answer but I am guess you are doing this as an assignment of sorts, so I just wanted to point you in the right direction. Use a python debugger (like PyCharm, or Wing IDE) to determine the differences between "5" + "10" 5+10 and [5]+[10]
You need to look into what's called Type Casting

difference between variable definition in a Haskell source file and in GHCi?

In a Haskell source file, I can write
a = 1
and I had the impression that I have to write the same in GHCi as
let a = 1
, for a = 1 in GHCi gives a parse error on =.
Now, if I write
a = 1
a = 2
in a source file, I will get an error about Multiple declaration of a, but it is OK to write in GHCi:
let a = 1
let a = 2
Can someone help clarify the difference between the two styles?
Successive let "statements" in the interactive interpreter are really the equivalent of nested let expressions. They behave as if there is an implied in following the assignment, and the rest of the interpreter session comprises the body of the let. That is
>>> let a = 1
>>> let a = 1
>>> print a
is the same as
let a = 1 in
let a = 1 in
print a
There is a key difference in Haskell in having two definitions of the same name and identical scopes, and having two definitions of the same name in nested scopes. GHCi vs modules in a file isn't really related to the underlying concept here, but those situations do lead you to encounter problems if you're not familiar with it.
A let-expression (and a let-statement in a do block) creates a set of bindings with the same scope, not just a single binding. For example, as an expression:
let a = True
a = False
in a
Or with braces and semicolons (more convenient to paste into GHCi without turning on multi-line mode):
let { a = True; a = False} in a
This will fail, whether in a module or in GHCi. There cannot be a single variable a that is both True and False, and there can't be two separate variables named a in the same scope (or it would be impossible to know which one was being referred to by the source text a).
The variables in a single binding set are all defined "at once"; the order they're written in is not relevant at all. You can see this because it's possible to define mututally-recursive bindings that all refer to each other, and couldn't possibly be defined one-at-a-time in any order:
λ let a = True : b
| b = False : a
| in take 10 a
[True,False,True,False,True,False,True,False,True,False]
it :: [Bool]
Here I've defined an infinite list of alternating True and False, and used it to come up with a finite result.
A Haskell module is a single scope, containing all the definitions in the file. Exactly as in a let-expression with multiple bindings, all the definitions "happen at once"1; they're only in a particular order because writing them down in a file inevitably introduces an order. So in a module this:
a = True
a = False
gives you an error, as you've seen.
In a do-block you have let-statements rather than let-expressions.2 These don't have an in part since they just scope over the entire rest of the do-block.3 GHCi commands are very like entering statements in an IO do-block, so you have the same option there, and that's what you're using in your example.
However your example has two let-bindings, not one. So there are two separate variables named a defined in two separate scopes.
Haskell doesn't care (almost ever) about the written order of different definitions, but it does care about the "nesting order" of nested scopes; the rule is that when you refer to a variable a, you get the inner-most definition of a whose scope contains the reference.4
As an aside, hiding an outer-scope name by reusing a name in an inner scope is known as shadowing (we say the inner definition shadows the outer one). It's a useful general programming term to know, since the concept comes up in many languages.
So it's not that the rules about when you can define a name twice are different in GHCi vs a module, its just that the different context makes different things easier.
If you want to put a bunch of definitions in a module, the easy thing to do is make them all top-level definitions, which all have the same scope (the whole module) and so you get an error if you use the same name twice. You have to work a bit more to nest the definitions.
In GHCi you're entering commands one-at-a-time, and it's more work to use multi-line commands or braces-and-semicolon style, so the easy thing when you want to enter several definitions is to use several let statements, and so you end up shadowing earlier definitions if you reuse names.5 You have to more deliberately try to actually enter multiple names in the same scope.
1 Or more accurately the bindings "just are" without any notion of "the time at which they happen" at all.
2 Or rather: you have let-statements as well as let-expressions, since statements are mostly made up of expressions and a let-expression is always valid as an expression.
3 You can see this as a general rule that later statements in a do-block are conceptually nested inside all earlier statements, since that's what they mean when you translate them to monadic operations; indeed let-statements are actually translated to let-expressions with the rest of the do-block inside the in part.
4 It's not ambiguous like two variables with the same name in the same scope would be, though it is impossible to refer to any further-out definitions.
5 And note that anything you've previously defined referring to the name before the shadowing will still behave exactly as it did before, referring to the previous name. This includes functions that return the value of the variable. It's easiest to understand shadowing as introducing a different variable that happens to have the same name as an earlier one, rather than trying to understand it as actually changing what the earlier variable name refers to.

How to duplicate a RuleContext

Is there any way to duplicate a ParserRule? I need a real deep copy, so copyFrom() doesn't do the trick. Or must I re-parse the code?
An alternative idea how to solve the following would also be much appreciated:
I am working on a compiler, translating old legacy code to modern programming languages, in this case EGL -> Java.
EGL has a concept called Standalone Function, which are similar to C-macros. This means that code inside the functions can reference symbols in the calling scope. So both defining and resolving of symbols and type promotion are context-dependent.
In ANTLR3, we solved this by dupTree(), and simply made a copy to work on in each calling scope.
Dynamic types is not an option.
Example (pseudo code) to illustrate:
Program A
int var = 4;
saf(); # Prints 5
end A;
Program B
String var = "abc";
saf(); # Prints abc1
end B;
function saf()
int j = 1;
print(var + j);
end saf;
As of version 4.2, ANTLR 4 does not include any API for manipulating the structure of a parse tree after the parse is complete. This is an area we are currently exploring, especially considering the possibilities created by the new pattern matching syntax.
For duplicating trees, I recommend you implement the visitor interface created when you generated your parser. This will allow you to call visit on any node in your parse tree to create a deep copy of that node.

Resources