Pytov – C-like syntax Python
github.com
github.com
int i = 100;
std::cout << i << std::endl;
if (1) {
int i = 200;
std::cout << i << std::endl;
}
std::cout << i << std::endl;
giving 100
200
100
They don't seem to serve such a purpose in Pytov. Eyeballing the source, it looks like you'd get this: i = 100
print (i)
if (True) {
i = 200
print (i)
}
print (i)
giving 100
200
200
So what's the point?Yes, you would - variables in Python have function-scope, not block-scope.
It doesn’t look like this project changes anything about Python except trivial bits of syntax.
int i = 100;
std::cout << i << std::endl;
{
int i = 200;
std::cout << i << std::endl;
}
std::cout << i << std::endl;
And since PyTov is supposed to be C-like, this might be a better example: #include <stdio.h>
int main(void) {
int i = 100;
printf("%i\n", i);
{
int i = 200;
printf("%i\n", i);
}
printf("%i\n", i);
return 0;
}
Also, the same would occur in C(/C++) if you didn't shadow the variable in the block scope and just called: i = 200It's not the curly braces that create the scope as such. It's the block, and the curly braces just happen to create the block. In C++ you can even do this, for example (I was surprised to find it doesn't work in C):
int i = 1;
if (true) int i = 2;
std::cout << i << '\n';
// Prints 1
In any case, the semanics of blocks (whether they create scopes) is totally orthogonal to the syntax of blocks. You can just as easily imagine the reverse: the current colon-indent syntax where blocks do create scopes.Another issue, which other replies have alluded to but not quite spelt out. You'd need to add a syntax to distinguish between regular assignment and declaration. In C you can still choose to assign to the outer-scope variable from a nested scope of you wish, so you don't want i=1 in a nested scope to always declare a new variable that shadows a variable in the outer scope.
Oh and one more thing. Your snippet (or equivalent) won't work in all C-style braces languages: in C# it is a compilation error to shadow a variable from an outer scope in the same function (presumably because it's a common source of bugs and extremely easy to just choose a different name for one of the variables).
That said, even if it gives me the syntactic sugar I crave I don’t know how much I’d be willing to trust a little-used language that compiles to something more common, because I’d worry about how its maintenance goes.
If the software goes unmaintained, would the syntax of Python outgrow it and break it? Maybe new features wouldn’t be usable? So while that syntactic sugar is indeed tasty, I don’t think this is what I need to get going in Python.
Java and C# are examples of well designed grammars. Tooling was a first class requirement for C# and it shows. Where python will never have great tooling.
That colon that starts every indented block is like an unmatched opening brace though.
There happen to be a handful of popular languages that are C-like in scope syntax, but it is important to get beyond the first thing you happened to learn. It is like only eating the cuisine from the country where you were born.
It may be popular, but it presents a number of challenges in Python and in other languages like YAML. If you're going to buck the C trend, at least make it somewhat ergonomic like Lua which I learnt recently and kind of like.
How do you feel about the user experience for Julia/Python interop?
Check out https://github.com/JuliaPy/PyCall.jl
It's the pragmatic choice.
And that produce visual noise and based on many formatting standards enforce wasted vertical space (curly on its own line) being able to see more code on screen without scrolling.
I have to type that whutespace. Whitespace indentation and line breaks are the most significant visual elements. Why would the compiler ignore that?
from __future__ import braces
SyntaxError: not a chanceI can imagine preferring Python syntax. I can't much imagine wanting Python slowness, which is inherent to the language semantics. People can disagree about syntax, but who wants slowness?
Converting is probably just a matter of a Makefile rule that would turn the code into standard C. It's just another code generator, like lex or yacc.
More like “python, but rah (bad),” from where I’m standing.
I’m surprised that there’s anyone who prefers brackets to indentation, unless the joke is going over my head. It’s much easier to spot an incorrectly indented segment than see that there are 3 closing brackets in one spot and 2 in another instead of 2 and 3 as it should be
def say_word(word): #{
print(word)
#}OptionalTheMoreYouKnow
Still love pythons syntax in general.
I once designed a Python-like language and used `function`, because the language made a distinction between `function`s and `method`s.
define
fac x
if
= x 0
1
* x
fac
- x 1
Python's keywords are sometimes shorter, but they are familiar to most Lispers, which some of the founders were.(meaning indentation, mostly)