tags:

views:

181

answers:

4

Are classes necessary for creating methods (defs) in Python?

+12  A: 

No. However, def's which aren't part of a class are usually called functions, not methods - but they are exactly the same thing, aside from not being associated with a class.

def myFunction(arg1, arg2):
    # do something here
Amber
You might argue they're not *exactly* the same, as static, class and instance methods behave differently as far as if and what is passed as the implied first argument.
Nathan Ernst
Nathan, I disagree. An unbound class method is simply a function. Only a *bound* class method has an implied first argument. Even then, it's just a function with a first argument curried into it.
Benson
+2  A: 

No, you can create functions using def without having to wrap them in classes.

If you are coming from a Java or C# background - where a class is required - you may want to read over An Introduction to Python: Functions or a similar article to understand how to work with functions in Python, as the language provides many other features such as first-class functions, returning multiple values, anonymous functions, etc.

Justin Ethier
A: 

I would say yes.

In python methods are defined in clases, functions are defined outside classes. Both are defined with def but they are in different namespaces. Methods are the functions of class instances

this is explained in the python reference

Callable types: These are the types to which the function call operation (see section Calls) can be applied:

User-defined functions

A user-defined function object is created by a function definition (see section Function definitions). It should be called with an argument list containing the same number of items as the function’s formal parameter list.

User-defined methods

A user-defined method object combines a class, a class instance (or None) and any callable object (normally a user-defined function).

joaquin
A: 

It depends on your definition of "method".

In some sense, no, classes aren't necessary for creating methods in Python, because there are no methods anyway in Python. There are only procedures (which, for some strange reason, are called functions in Python). You can create a procedure anywhere you like. A method is just syntactic sugar for a procedure assigned to an attribute.

In another sense, yes, classes are necessary for creating methods. It follows pretty much from the definition of what a method is in Python: a procedure stuck into a class's __dict__. (Note, however, that this means that you do not have to be inside a class definition to create method, you can create a procedure anywhere and any way you like and stick it into the class afterwards.)

[Note: I have simplified a bit when it comes to exactly what a method is, how they are synthesized, how they are represented and how you can create your own.]

Jörg W Mittag
Oh, I can supply the “strange” reason: they're called “functions” in C too. Like functions in C and in Pascal, Python functions always have a return value. There are no Pascal-style “procedures” in Python. Now, what exactly is the meaning you assign to “functions” and “procedures”?
ΤΖΩΤΖΙΟΥ
@ΤΖΩΤΖΙΟΥ: There are many equivalent ways to define what a function is. Roughly speaking, a function consists of a triple `(D, C, F)` of sets, where `D` is the domain of the function, `C` is the codomain, and `F` is a set of pairs `(a, b)`, such that `∀(a, b): a ∈ D ∧ b ∈ C ∧ every e ∈ C is the first element in exactly one pair`. Python's procedures violate the last condition, therefore they aren't functions. A procedure is basically the same as above, except that the last condition doesn't need to hold and it can have side-effects. (Those two conditions are actually the same.)
Jörg W Mittag
Python is not math (and you should know that), neither are several other programming languages happily calling subroutines as “functions”. If I may veer off, brake pads have a totally different function in cars which no-one calls strange, and you should function rather differently in stackoverflow.com than in mathoverflow.net. Finally: practicality beats purity :)
ΤΖΩΤΖΙΟΥ