views:

382

answers:

4

Or is everything a method?

Since everything is an object, a

def whatever:

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

A: 

You can have functions in python, as not everything is an object, and files (packages) do not coincide with classes either.

mac
In Python, everything is an object. http://www.diveintopython.org/getting_to_know_python/everything_is_an_object.html
Mark Byers
"Everything is an object": yeah, maybe, there are opinions... However: (1) it is irrelevant to the question: "are there function in python?". Answer: "yes". (2) The "everything is an object" in python has been debated at length [See Ruby vs. Python] (Pilgrim himself specify that "everything is an object" applies if you stick to the definition of object given by the python documentation). (3) If by thinking "everything is an object" you can't see how to do stuff in a procedural way (as it looks from the OP' question) and apply a java mindset than "eiao" is just a purposeless mantra. Cheers! :)
mac
[The answer given by Dave is however a much better answer, exactly because it explains well how EIAO does not mean you can't have functions]. (+1!)
mac
A: 

Hmm... You can work with "whatever" as an ordinary function in file namespace.

demas
+30  A: 

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.)

Dave Webb
+2  A: 

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.

Tendayi Mawushe