A simpler way to write def

Omitting def can result in more concise functions.

hello:
    print(f"Hello {name}")

hello(name="World")  # Hello World

Complex writing

def hello(name):  # Still have to write (name); it's unnecessary.
    print(f"Hello {name}")

hello(name="World")

If you it on one line then it’s ambiguous though.

hello: print(f"Hello {name}")

Is that a function, or an annotation of the variable hello?

1 Like

With just two lines of code, misalignment can occur.

def_name:pass
    pass

This would conflict with a simpler way to write class.

A(str):
    def hello(self):
        print(f"Hello {self}")

And since omitting “class “ will save typing six keys, while omitting “def “ will only only save typing four keys, the former wins.

9 Likes

A class will contain many def.

Why is including the name unnecessary? Are you suggesting that hello() and hello(name) are the same?

Why is it necessary to include the (name)? Isn’t it okay to leave it out?

It’s great to think about language design and ways Python could be improved. But it’s important to consider the stability of the language, the cost of a change, whether the benefits justify the turmoil, and all of the edge cases.

Your proposal was:

hello:
    print(f"Hello {name}")

hello(name="World")  # Hello World

How do we know that name is a parameter to the function? What is the short version of this function:

greet = "Hello"
def hello(name):
    print(f"{greet} {name}")

You can’t assume that every unassigned variable in a function is a parameter. They have to be explicitly declared. As an example, your definition had two names in the body: print and name. Why was name a parameter but print was not?

You have to think about the detailed semantics of a language change to be sure it makes sense and has strong benefits. Consider uses beyond what you’ve experienced in your code. Look at other code to see how it would be affected.

13 Likes

Also, “explicit is better than implicit “. I think the 3/5 letters for def/ class are well spent. - Though one could argue why it’s def and not func or similar, but that ship has sailed.

3 Likes

Define the default variable for a parameter

hello:
    print(f"{greet="Hello"} {name}")


hello(name = "World")                 # Hello World

hello(greet = "Hi", name = "Python")  # Hi Python

Nobody would define a print parameter:

def hello(print):
    print(f"Hello {print}")

hello(print="World")  # Error

Within a function, simply using {} determines that it is a function parameter.

-1. Use of this style would make code harder to read. It would also make it difficult to search for definitions of functions.

5 Likes

explicit is better than implicit writing:


int python = 4;

More modern writing


python = 4

I think def is fine. It’s concise, you can type it with one hand, and it sidesteps the problem of whether to call it a “function”, “procedure”, “subroutine”, etc. :slight_smile:

Traditional syntax makes it easier to read

def hello(name="World"):
    print(f"Hello {name}")

hello(name = "Python")

More modern syntax:

hello:
    (name = "World")
    print(f"Hello {name}")

hello(name="Python")

or URL writing

hello? name="World"
    print(f"Hello {name}")

hello(name="Python")

or

hello:
    (name = "World")
    print(f"Hello {name}")

hello?name="Python"

We need innovation; cannot stay like this for life.

And being easy to read is one the wonderful things about Python.

No, we don’t.

6 Likes

We shouldn’t avoid a better approach just because the traditional one is easier to read.

My writing looks more like code block.

Define “better”. If the traditional one is easier to read, and your proposed alternative achieves the same thing, how is it better? What’s the improvement?

If you want to change just because Python’s syntax is old, that’s fine! There’s nothing wrong with that. The new syntax won’t be Python though - it’ll be some entirely new language, incompatible with Python. To get started on that, I would recommend learning about language grammars and grammar compilers, and build yourself a program that can read your new language and output something in a more traditional language (sometimes called a “transpiler”). You then have the freedom to make your new language follow whatever rules you like. This is an INCREDIBLY useful exercise; you will learn an enormous amount about what is good, what is bad, what is ugly, and especially, how backward compatibility works (once you start using your own language and have a few scripts around).

2 Likes

This is not a brand-new syntax.
This is a simpler way to write def .

similar int python = 4; and python = 4

You haven’t yet provided a detailed description of the semantics you are proposing. Until you do, this isn’t a serious proposal. How are the parameters of the function determined? What is the new form of this function?

import random
greets = ["Hello", "Hey there"]
punctuation = "!"

def hello(name, number):
    salutation = random.choice(greets)
    print(f"{salutation} {name}{number * 2}{punctuation}")

Inside the function are five names to be looked up: random, greets, print, name, number, and punctuation. The f-string uses four names. Two of them are parameters. How would implicitly defined parameters be determined?

To be direct: I don’t think this is a serious proposal. Determining parameters by what is used in f-strings is a tiny slice of how functions are actually written.

We aren’t going to change for change’s sake. No matter what you say, this is a new syntax, with little specification, and no real benefit.

3 Likes