473,378 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.

Mar 7 '06 #1
5 7679
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.

Mar 7 '06 #2
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

Mar 7 '06 #3
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 'o****@xiludom.gro'.split('@')])"
Mar 7 '06 #4
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 !-)
Mar 7 '06 #5
ak*********@gmail.com 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'

Mar 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
2
by: F. Petitjean | last post by:
I have written a script to find the modules which export the largest number of names. The gc.getreferrers(*objs) function gives also an idea of the dependencies between the modules. The code...
9
by: jlopes | last post by:
I'm looking at the differences between these to forms and see no difference in their use. When accessed through a derived class. class ABase { public: virtual void filter(){ /* some code */ }...
4
by: s.subbarayan | last post by:
Dear all, How different is a header file from a library file in C?Under what circumstance u go for library and under what circumstance u go for header?Afaik,both have some declarations which can...
6
by: Sky Sigal | last post by:
Uh...a bit ashamed to admit this now that I have been programming in C# for a while...but I still don't clearly understand from the documentation what the difference between an Assembly and a...
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
6
by: krishnakant Mane | last post by:
hello, I am strangely confused with a date calculation problem. the point is that I want to calculate difference in two dates in days. there are two aspects to this problem. firstly, I can't get...
4
by: sniipe | last post by:
Hi! I am new in Python, so I count for your help. I need to get difference in months between two dates. How to do it in python? I am substracting two dates, for example date1 - date2 and I got...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.