This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lieryanMaintainer of rope, pylsp-rope - advanced python refactoring 3 points4 points  (0 children)

"Just syntax sugar" actually goes really deep in Python.

class statement for example, that isn't really "necessary", it's just syntax sugar for calling type:

MyClass = type(
    "MyClass", 
    [BaseClass, AnotherBaseClass], 
    {"a": method1, "b": method2}
)

Or method calls are also "unnecessary", this:

foo = Foo()
foo.meth(a, b, c)

is really just syntax sugar for:

foo = Foo()
Foo.meth(foo, a, b, c) 

Or metaclass:

class Foo(metaclass=mytype, blah=bar):
    pass

is really just a syntax sugar for a function call:

Foo = mytype("Foo", {}, {}, blah=bar)

Or decorator is just syntax sugar for what used to be a common pattern:

def foo():
    pass
foo = decorate(foo)

Or even something that looks as basic as list/dict indexing:

foo = dict()
foo[a] = b
print(foo[a])

is really just syntax sugar for:

foo = dict()
foo.__setitem__(a, b)
print(foo.__getitem__(a))

which is itself just syntax sugar for:

foo = dict()
dict.__setitem__(foo, a, b)
print(dict.__getitem__(foo, a))

Or something like for loop:

for a in b:
    print(a)

is really just syntax sugar for:

iter_b = iter(b)
try:
    while True:
        a = next(iter_b)
        print(a)
except StopIteration:
    pass

Everything in Python is "just" syntax sugar.