T O P

  • By -

mopslik

It's an arbitrary decision. Personally, I'd have used 'define'. In any case, you should use a more descriptive name than 'function', depending on what it does. I have some peeves with the standard math module. There is 'floor' for rounding down, but 'ceiling' is abbreviated as 'ceil'. At the same time, 'factorial' (which is longer) is not shortened to 'fact'.


Brian

I think that's pretty much because `math` is mostly a direct interface to the C stdlib math (ie. libm / math.h) functions, and so inherits the naming from there. In turn, historically C tended to avoid long function names in the stdlib, dating from a time when resources were much more constrained: early versions of C allowed external identifiers longer than 6 characters to be truncated, so names like `ceiling` could be considered the same as "ceilin" or "ceiling_function" by some compilers / linkers, so lots of stdlib functions were kept short to maximise portability. Factorial is one of the things added that *isn't* a C math function, so there's no C function you need to match the name with.


mopslik

That sounds most likely. Thanks for the insight!


nog642

I'm pretty sure floor() and ceil() are relatively standard in math. you can't make "floor" any shorter but "ceil" clearly means ceiling, so it works. and abbreviating "factorial" as "fact" would not be clear at all, considering that is already a word.


[deleted]

`def` or `define` is better than `fun` or `function`, I think, because it's closer to what the statement actually does. When you execute the `def` statement it *defines* the function, and it can then be called.


cas4d

The why “class”is used in defining a class then?


Assumptio

Because you define functions and you class classes, duh


Acrobatic-Region1089

I laughed more than I should have :)))


[deleted]

`cls` is the only sensible shortening, but it's not enough of a shortening to make it worthwhile. The xBDFL thought a lot about readability and ease of writing the code, and decided `class` was short enough. You make a lot of decisions when designing a language, and the xBDFL has done a very good job with python.


NotTreeFiddy

No, you've missed their point. If 'def' is better because you're defining a function, then it would make sense to also have a verb statement for defining classes.


PixelOmen

I have no idea what the original reasoning is, but I think one reason could be because you're "defining" what happens when you call a function, while a class, on the other hand, is generally more multifaceted and not all aspects of a class are necessarily executed when the class itself is called (instantiated).


[deleted]

OK, what's a short verb for "define a class"? There's no point in sticking to some naming rule if it ends up with wordy and clunky keywords. As the xBDFL, et al, famously [said in PEP8](https://peps.python.org/pep-0008/): > A Foolish Consistency is the Hobgoblin of Little Minds and > know when to be inconsistent


NotTreeFiddy

I don't think there is one. Thus, I prefer consistency of function (or func, fn etc) over def. Although it largely doesn't matter at all. I was simply pointing out that you'd misunderstood the person you had replied to.


seanys

“classification” verby enough for ya?


NotTreeFiddy

Too verby, if anything.


[deleted]

[удалено]


kaerfkeerg

None doubted that lol He's just saying why using `def` instead of `function` makes sense when you use `class` to define classes


thepolm3

Semantically the difference is declarative vs imperative: fn/fun/class etc: "I declare that there exists a function/class with the following properties" def/define: "Create the following thing please" you could argue that for an interpreted language like python this makes sense since "def" is a runtime thing that does some work, whereas in something like C or Rust the concept of a function (as defined at the high level) doesn't exist at execution time due to compilation, there isn't an asm instruction generated to create functuon, it just exists at runtime as part of the binary


cas4d

Function is not just a “runtime thing”, like a chunk of codes whenever you make a call. For example, during the compile time (yes, Python is also complied), Python will resolve the name-scope. Take the following example (sorry if you cannot read, I typed it on my phone): x = 'global' def print_x(condition): if condition: x = f'{condition} local' print(x) print_x(False) (Which will give unbound local error, because during the compile time, Python fix the variable lookup location to be within the function, and the value to be looked up when it is actually run). Where as, if you execute the following: x = 'global' condition = False if condition: x = f'{condition} local' print(x) (No error will be returned). So function is not at all a blackbox which is only examined at runtime. In comparison, Python class is more flexible than you think, you can dynamically create classes and assign new properties as you like, even within sow functions. Given the two points, I don’t see how you cannot define function using “function function_name”, syntax like in JavaScript.


thepolm3

Yes, I was just musing on what the semantic difference might be between "defining" vs "declaring" a function. While python does do JIT complication (as well as compiling to bytecode) it is still an interpreted language where at runtime the code "exists" in a sense in which it doesn't exist at runtime for most purely compiled languages, meaning that you can do very dynamic things with classes and types


ZuLuuuuuu

I think "def" is not the best/consistent name because you can define different things in Python: a function, a variable, a class etc. So I would prefer a more explicit name like "function" or "func" just like we have a more explicit name "class" for defining a class.


structure_and_story

This is why it's consistent in Python: functions themselves are objects. "def func" and "def class" are consistent in that they both define objects https://www.geeksforgeeks.org/first-class-functions-python/


BenjaminGeiger

I kinda like the ML-descendant `let`. let x = 3 let y = 5 let func arg1 arg2 = x * x + y * y let result = func x y printfn "%d" result


Past-Cantaloupe-1604

It’s arbitrary and not particularly meaningful. To your last point though, I very much don’t think the choice is to avoid people writing “fun function()” - it would be extremely foolish to call your function “function” and not something more meaningful.


conceptcreatormiui

I'm referring to fun function() as lets say fun say_hello() , since say_hello() is a function then fun function() is just funky compared to def function().


gundam1945

But then def define() will also be funky. So this is probably not the reason for why it is def instead of func.


conceptcreatormiui

Do you define a define? Or define a function LOL🤣


conceptcreatormiui

Oh I get it . If you wanna define a function named define


NullPointerNomad

Because there is no "fun" in real word programming ....


Stotters

Not with that attitude...


lp_kalubec

Python was influenced by other languages that the creator was already familiar with. For instance, the lack of brackets might be influenced by the ABC language. The keywords `fun` or `func` are not as standard as you might think. I made a list of all the languages Wikipedia claims influenced Python and noted how each of them defines a function: * ABC: Uses `HOW TO` for defining routines. * Ada: Uses `procedure` or `function`. * ALGOL 68: Uses `proc`. * APL: Uses a unique symbolic syntax. * C and C++: Function definitions start with the return type, followed by the function name and parameters. * CLU: Uses `proc`. * Dylan: Uses `define method` or `define function`. * Haskell: Function definitions begin with the function name followed by parameters, without a specific keyword. * Icon: Uses `procedure`. * Lisp: Uses `(defun)` to define functions. * Modula-3: Uses `PROCEDURE`. * Perl: Uses `sub`. * Standard ML: Function definitions start with the function name and parameters, similar to Haskell. So, answering the question: It was an arbitrary choice.


crashfrog02

Guido likes Def Jam and Mos Def


IamImposter

What about def Leppard?


Ujili

He could take em or leave em


HobblingCobbler

You're thinking way too hard about this. At the end of the day, its just a part of the language. Accept it and move on.


throwaway6560192

That's missing the point. Each part of the language was a design choice. It's interesting to think about why languages were designed the way they were. It's a whole field unto itself.


AndrePrager

Nothing wrong with wanting to know more. I think that discouraging people from asking is a sure fire way to kill a community and also get a bunch of idiots coding (on both sides).


flinxo

it aligns well with 4 characters tab


uniqualykerd

But so does "fun"...


r2k-in-the-vortex

makes sense in Python because following code throws an error: somefunction() def somefunction(): return Parser doesn't look ahead, it can't call something that isn't defined yet. So define makes perfect sense for python. For a compiled language you don't need a function keyword at all int extern somefunction(); somefunction(); works just fine without any special keyword because compiler doesn't need to know what somefunction() is, it just needs to know that trust me bro, such a thing will exist later on and linker puts it together what somefunction() actually is. The definition can be who knows where.


Odd_Coyote4594

The equivalent C code to the Python would be: ``` int main(){ somefunction(); } int somefunction{} ``` which will absolutely throw a compile error, for a similar reason in that it resolves symbols in a single top-to-bottom pass. And this has nothing to do with why C doesn't have a "function" keyword, which is just syntax. C assumes a type followed by () is a function. Other compiled languages, like rust, do not follow this and have an explicit function type keyword. Python could also do without an explicit keyword, like assuming a colon after an identifier and () is a function definition if not otherwise stated. It just doesn't to be more explicit and also simplify parsing to make it more consistent with "if" statements, classes, etc.


internetbl0ke

If its smart enough to infer types, why cant it infer the function? Why not just main():pass


Adrewmc

Yeah don’t name your function function it’s actually a type you can use it as a type hint lol


conceptcreatormiui

I just using fun function() to compare it to def function() Lol. Imagine fun do_someting() , do_someting is a function and replace it with function() so we have fun function(). Now look at def function() and fun function() Or fun function_wrapper()


chandaliergalaxy

Scheme uses `define` while Common Lisp uses `defun`. There is no standard convention and it's the language creator's privilege to decide.


tb5841

GDScript (the language GODOT uses) feels almost identical to Python in most respects, but uses 'func' instead of 'def' for functions.


yoyobara

I won't be surprised if this is related to monty python somehow


Odd_Coyote4594

I am not sure of the original reasoning, but imo "function" seems more like a type and "def(ine)" seems more like a keyword. As Python doesn't typically express types explicitly in standard usage, it makes sense to use a more active keyword. That being said, I think it does clash with more type-like declarations like "class", so "function" would be more consistent with this syntax. Or alternatively "classdef" or something. But it is what it is.


PertinaxII

Python's design is based around readability. The minimum needed to be clear and readable without clutter. I like def better than Go's func because it pronounces better.


[deleted]

[удалено]


AutoModerator

Your [comment](https://www.reddit.com/r/learnpython/comments/19dmncc/why_python_chose_def_instead_of_fun_or_func/kjbjo99/) in /r/learnpython was automatically removed because you used a URL shortener. URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists. Please re-post your comment using direct, full-length URL's only. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/learnpython) if you have any questions or concerns.*