Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am currently teaching myself Python, using the GNU licensed book "Introduction to Computer Science using Python."

In chapter 3, functions are covered. While I understand the concept of functions and how they simplify code by executing a defined sequence of statements whenever they care called, and that Modules are collections of related functions and variables, stored in a file, I do not understand the concept of module objects and function objects. For example, when "import math" is executed, it says, a module object is created. Also, when a function is defined and is executed, a function object is created.

Could someone please explain what these "objects" are to me?

share|improve this question
add comment

2 Answers

Python at run-time keeps a lot of information around about the state of the code it's executing, and exposes quite a lot of it to programmers. So, at run time, there is an actual object (an instance of a class) called a module object that is created when a module is imported. It is a genuine Python object and you can do anything with it that you can do with a Python object. It has methods and everything.

When a function definition that gets executed (which isn't the same as the function being executed - normally the definition would be executed once for the function when the module is imported) it creates a function object, which is just a special kind of object that is a wrapper around the executable code. This too is a real object, you can set variables to it, pass it as a parameter, call it's methods, etc.

share|improve this answer
    
Thank you for your response. As this is a text on Computer Science and not on programming, it hasn't introduced the concept of objects and classes yet. If it's not too much trouble, could you please explain to me what an object and a class is? What I currently understand about classes is that when you use the "type" function in Python 3.2, it shows the type of the value whether it be an integer, float, or string, but in the program it refers to it as class for some reason. For example, "type('32')" would yield something like "class <string>". Please shed more light on this if I'm wrong. –  qzxt Oct 28 '11 at 1:19
    
@HarryRiddle: Nothing you said is wrong. But there's an awful lot to be said about classes and objects, especially when one wants to actually grok the ideas behind object-oriented programming. The whole topic is both far too complex to cover in a single answer (let alone comment) and far too vital to try it anyway and screw up. –  delnan Oct 28 '11 at 1:24
1  
@HarryRiddle - The question of what an object and class is unfortunately lies outside the scope of this site because 1. You could write a book on it (there are many such books), and, arguably, 2. This is meant to be a site for expert programmers and it's a basic question that can be answered by basic research. I just did a quick search and found a lot of resources that in general should do a better job than I would explaining these concepts. (I searched "definition object and class" but many searches would work). Python also has lots of documentation readily available. –  psr Oct 28 '11 at 17:34
add comment

A function object refers to the fact that Python treats functions first class objects. This allows Python to use certain techniques from functional programming, particularly the passing of functions in ways similar to how you would pass any other object like a String or an Array.

A module object is an object created that represents a library of functions. It would be similar to an Abstract Class in a more Object Oriented Language. You'd import in to get sets of predefined functions that you might need to use.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.