Difference between a library and a module...
OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? And if i do string.replace() am I using a module or a function or a method or what? Sorry. -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between a library and a module...
I'm not 100% sure what is a library in python. Your example above is importing a module. Someone else can correct me, but I use libraries to refer to underlying c/c++ code that is required for the python modules to function. So in pure python you are really only dealing with modules. string.replace() I'm 90% sure is a function in the string module. However something like this: foo = bar foo.Capitalize() bar.capitalize is executing a method. Actually at this point string.replace() may be a method as well, I don't know for sure as I haven't inspected the string module's code. Read some intro to OOP, for a better understanding, but the main difference between a function and a method, is that a method is associated with some class or object. In Python it's really only objects (even class is an object) Hence when I created the string object foo, and executed Capitalize() it was a method on the string object. the same thing as a function might look something like: # defining a function def capitalize(inStr) #do stuff here to capitalize the string return outStr foo = capitalize(bar) hope this helps. -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between a library and a module...
sophie_newbie wrote: OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? I'm not a guru, but... I think that modules are things that live inside the Python language. In the above case, you are importing a Python module. I think that a library is something that resides on the file system and contains code. But it can be anything, and it exists outside a Python program. I have the feeling that a library is usually lives in compiled form, while a python module can be anything that can be 'import'-ed (py file, pyd file or an so file...) And if i do string.replace() am I using a module or a function or a method or what? What you call here is: string.replace. In the standard string module, replace is a function. But if string refers to a custom module, then string.replace could be a class or an object (or any callable) as well. By the way, modules are not callable at all. Methods can only be called with an object. Class methods can be called with a class. Well, a module is itself a special object, called the 'module object'. Module objects have no class, and they cannot be instantiated or called. http://docs.python.org/ref/types.html#l2h-105 I hope this helps. Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between a library and a module...
sophie_newbie wrote: OK this might seem like a retarded question, Better to look like an ignorant than to stay one !-) but what is the difference between a library and a module? Python only defines 'modules' and 'packages'. A module can technically be any python source file, but usually refers to a python source file that defines symbols (variables, constants, functions, classes...) and is meant to be imported (vs. a 'script', which is meant to be executed). A package is a kind of a super-module - a collection of modules and packages -. To make a package, just put your modules in a folder and add a __init__.py file (which can be empty, the mere existence of this file is enough to turn your folder into a python package) 'librairy' is a non python-specific, more or less formal term that refers to a collection of functions, classes, variables etc... (just like a (real) library is a collection of books). In Python, 'library' can apply either to an external system lib (.dll on Windows, .so on *n*x), a collection of packages and modules, a single package, or even a single module... If I do: import string am I importing a module or a library? Could be a module, a package, or a module wrapping an external lib (AFAIK, the string module is a wrapper around a system lib). The term 'module' refers in fact to two things: the physical python source file, and the python object created from it by an import statement. When importing a package, Python creates in the current namespace a module object[1] from the __init__.py file. Any symbol defined in the __init__.py will become available as an attribute of the module object. Of course, if the __init__.py is empty, this won't give you much !-) And if i do string.replace() am I using a module or a function or a method or what? In this case : string.replace() is the function 'replace' defined in the module 'string'. More generally: when you import a module (or package FWIW, cf above), Python creates a module object. Symbols defined in the (physical) module become attributes of the (object) module. Some of these attributes are 'callable' (functions, classes,... ). Sorry. Why ? HTH -- bruno desthuilliers python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between a library and a module...
Laszlo Zsolt Nagy a écrit : sophie_newbie wrote: OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? I'm not a guru, but... I think that modules are things that live inside the Python language. a (python) module is two things (depending on the context): either a python source file or a (compiled) system library ('something that resides on the file system and contains code', isn't it ?), and (once imported by the interpreter) it's representation at runtime as a python object. (snip) I have the feeling that a library is usually lives in compiled form, while a python module can be anything that can be 'import'-ed (py file, pyd file or an so file...) Some python modules are in fact coded in C then compiled as system librairies (.so on *n*x, .dll on Windows). So there's no clear technical distinction here. AFAIK, librairy originally refers to system libs, but we also talk about the standard python library, which is a collection of Python (or system lib) modules and packages. By the way, modules are not callable at all. Methods can only be called with an object. Class methods can be called with a class. Well, a module is itself a special object, called the 'module object'. Module objects have no class, Python 2.4.1 (#1, Jul 23 2005, 00:37:37) [GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2 Type help, copyright, credits or license for more information. import deco deco module 'deco' from 'deco.py' deco.__class__ type 'module' deco.__class__.__name__ 'module' Seems like they do have one... and they cannot be instantiated deco.__class__('foo') module 'foo' (built-in) import types types.ModuleType('bar') module 'bar' (built-in) I hope this helps. So do I !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between a library and a module...
[EMAIL PROTECTED] a écrit : I'm not 100% sure what is a library in python. Technically, nothing. string.replace() I'm 90% sure is a function in the string module. it is. However something like this: foo = bar foo.Capitalize() s/C/c/ bar.capitalize is a method. ...which is usually built from a function. Read some intro to OOP, for a better understanding, but the main difference between a function and a method, is that a method is associated with some class or object. Note that (part of) this association is made at runtime. Before you try to access it, it's a function (usually defined in the namespace of the class). When you try to access it, it's wrapped into a MethodWrapper object, that turns it into a method. In Python it's really only objects (even class is an object) Hence when I created the string object foo, and executed capitalize() it was a method on the string object. the same thing as a function might look something like: # defining a function def capitalize(inStr) #do stuff here to capitalize the string outStr = inStr[0].upper() + intStr[1:].lower() return outStr foo = capitalize(bar) Defining a method is really just defining a function: class StringWrapper(str): pass ... s = StringWrapper('foo') s 'foo' def capitalize(s): ... print yaoo, I was just a function, ... I'll be promoted to a method ... try: ... return s[0].upper() + s[1:].lower() ... except (IndexError, AttributeError, TypeError), e: ... return too bad, could not capitlize %s : %s % (s, e) ... StringWrapper.capitalize = capitalize s.capitalize() yaoo, I was just a function, I'll be promoted to a method 'Foo' -- http://mail.python.org/mailman/listinfo/python-list