Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there functions in Python, or is everything a method?

Or is everything a method?

Since everything is an object, a

def whatever:

is just a method of that file.py, right?

like image 395
TIMEX Avatar asked Nov 30 '09 11:11

TIMEX


People also ask

Does Python have functions or methods?

But Python has both concept of Method and Function. Method is called by its name, but it is associated to an object (dependent). A method definition always includes 'self' as its first parameter.

Is every method a function?

Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program.

Are methods just functions?

A method, like a function, is a collection of instructions that perform a task, but a method is associated with an object and a function is not.


2 Answers

Python has functions. As everything is an object functions are objects too.

So, to use your example:

>>> def whatever():
...     pass
...
>>> whatever
<function whatever at 0x00AF5F30>

When we use def we have created an object which is a function. We can, for example, look at an attribute of the object:

>>> whatever.func_name
'whatever'

In answer to your question - whatever() is not a method of file.py. It is better to think of it as a function object bound to the name whatever in the global namespace of file.py.

>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__d
oc__': None, 'whatever': <function whatever at 0x00AF5EB0>}

Or to look at it another way, there's nothing stopping us from binding the name whatever to a different object altogether:

>>> whatever
<function whatever at 0x00AF5F30>
>>> whatever = "string"
>>> whatever
'string'

There are other ways to create function objects. For example, lambdas:

>>> somelambda = lambda x: x * 2
>>> somelambda
<function <lambda> at 0x00AF5F30>

A method is like attribute of an object that is a function. What makes it a method is that the methods get bound to the object. This causes the object to get passed to the function as the first argument which we normally call self.

Let's define a class SomeClass with a method somemethod and an instance someobject:

>>> class SomeClass:
...     def somemethod(one="Not Passed", two="Not passed"):
...         print "one = %s\ntwo = %s" % (one,two)
...
>>> someobject = SomeClass()

Let's look at somemethod as an attribute:

>>> SomeClass.somemethod
<unbound method SomeClass.somemethod>
>>> someobject.somemethod
<bound method SomeClass.somemethod of <__main__.SomeClass instance at 0x00AFE030

We can see it's a bound method on the object and an unbound method on the class. So now let's call the method and see what happens:

>>> someobject.somemethod("Hello world")
one = <__main__.SomeClass instance at 0x00AFE030>
two = Hello world

As it's a bound method the first argument received by somemethod is the object and the second argument is the first argument in the method call. Let's call the method on the class:

>>> SomeClass.somemethod("Hello world")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method somemethod() must be called with SomeClass instance as first argument (got str instance instead)

Python complains because we're trying to call the method without giving it an object of the appropriate type. So we can fix this by passing the object "by hand":

>>> SomeClass.somemethod(someobject,"Hello world")
one = <__main__.SomeClass instance at 0x00AFE030>
two = Hello world

You might use method calls of this type - calling a method on a class - when you want to call a specific method from a superclass.

(It is possible to take a function and bind it to class to make it a method, but this is not something that you'd normally ever need to do.)

like image 154
10 revs, 2 users 97% Avatar answered Oct 13 '22 16:10

10 revs, 2 users 97%


Unlike in Java in Python a file named file.py does not necessarily contain a class called file as you might expect if this was a java file named file.java.

In Python a file is a module which is really just a namespace (more comparable to a Java package than a Java class) and not a class. Consider the example file.py below:

def whatever_func():
    print "What ever function"

class SomeClass(object):
    def whatever_meth(self):
        print "What ever method"

In the example above the file module/namespace contains an ordinary function named whatever_func and a class SomeClass which has a method whatever_meth.

like image 34
Tendayi Mawushe Avatar answered Oct 13 '22 16:10

Tendayi Mawushe