473,753 Members | 7,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7724
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(inSt r)
#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.ModuleTyp e('bar')

<module 'bar' (built-in)>

I hope this helps.

So do I !-)
Mar 7 '06 #5
ak*********@gma il.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(inSt r)
#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(s tr): 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.c apitalize = 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
1871
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
2
2440
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 (statsmod.py) : #!/usr/bin/env python # -*- coding: latin-1 -*- """
9
1878
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 */ } }; class D_of_ABase : public ABase
4
2604
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 be called from a source file.And what should I do if i want to build a library?Is it that jus setting the compiler option to be library makes u to create a library or u have to do something in the code to create a library? I am sorry if it looks...
6
15841
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 Module, and when I would use anything else than Assembly for reflection purposes... Assembly is the actual dll, or exe file, right? Module is...? A subset of an assembly? a superset? Nothing to do with it at all?
23
14056
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 didn't work (the timedelta class doesn't have a year accessor). I looked in the docs and the cookbook, but I couldn't find anything, so
6
4031
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 types of objects (classes) can only be defined in Class modules.
6
9022
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 a way to convert a string like "1/2/2005" in a genuan date object which is needed for calculation. now once this is done I will create a another date object with today = datetime.datetime.now() and then see the difference between this today and...
4
6655
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 result in days, how to change it? Best regards
0
9072
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9451
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9421
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4771
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3395
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2284
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.