Def and class words like an option

I downloaded a one project written in Python. I searched over the all files for this pattern ):
I noticed that I encounter this pattern in particullar places, mostly for the functions and the classes decalration.
def func_name():
class ClassName(Param):
So I wonder, do we really need these words def and class for decalration to defenitely say that we have a function or a class declaration?
The colon following after parenthesis is enogh to say we are not calling function, but declaring it. And name spelling can give us an information what we are declaring class or function.
So maybe it is possible to make words def and class optional?

Whether they are needed technically isn’t relevant. They are needed to make Python code readable for humans.


No, it cannot. This is against the fundamental design of Python in many ways.

Also, there are many other statements that can end with ):. For example, nothing prevents putting an if condition inside parentheses.

yes it is, but I’m sayng about calling and declaring finction and class

Okay, and why are those two things so special that they should have this optional way to write them?

It would also make it almost impossible to grep for function or class definitions. It would definitely not improve the language.

def func_name():
class ClassName(Param):

I think it is a matter of habit, if language had this approach from the begining I believe there was no any confusion

I agree it is much easier to find class and def words, but using regular expressions for search might be helpfull

Exactly how do you distinguish between a function and a class definition then? Just by snake casing versus camel casing? That is going to be so unreliable on so many levels. Many built-in “functions” are also implemented as classes.

1 Like

redundancy, why should I type additional words, if I might ommit them?

Then why does the same argument not apply to if?

I agree, with you, that’s why I propose to make it optional. If we take into account this problem, it is stiil possible to make using word def optional, but not class

OK, if the omission applies to only function definitions then it may at least be unambiguous.

Note that the original K&R C also allowed an implicit return type in a function definition:

main(a, b, c, d){
    printf("%d", d);

can you determine what declaration is here while or if ?

1 Like

I think you got the cases switched in a way. In most cases, we’re passing in parameters or arguments to functions, and in most cases, classes aren’t inheriting from parent classes.
So regular code would be something like

func_name (a,b,c):

Also are you suggesting the func_ becomes mandatory? Cause how then would you tell the difference between a class and function definition if they all just had one name?

func_add (a,b):     or      add(a,b):
ClassAccount:      or     Account:

And if func_ and Class are necessary prefixes, doesn’t that become the same as just using def and class as prefixes? How about in cases where both the class and function have parentheses placed, like when a function is accepting arguments and classes inherenting from parent classes

add(a,b):                     #function
add(parent1, parent2):  #class

How would the parser tell the difference between the two? Unless you wanna enforce case sensitivity between class definitions and function definitions?


Of which it would clash with the rest of the language constructs.

No, just common approach to naming

I didn’t say that

The spelling for the function names and class names you saw is a convention which is most often, but not always followed: it is not a requirement for Python. Consider the following, with your notation:


Which one of these four possibilities does this mean?

Case 1:

def name1(arg1):
    def name2(arg2):

Case 2:

class name1(arg1):
    def name2(arg2):

Case 3:

def name1(arg1):
    class name2(arg2):

Case 4:

class name1(arg1):
    class name2(arg2):

All four possibilities are valid in Python.

Note that Python code can and is written using names in languages other than English, where there might not be such concepts as upper/lower cases, which would make it impossible to enforce a convention distinguishing names of classes and functions based on the spelling.

1 Like

at least def can be omitted in this case

Tell me, was that decision regarded as a good one or a bad one? Is it a good precedent to follow?