Difference between a library and a module...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sophie_newbie

    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.

  • akameswaran@gmail.com

    #2
    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(inSt r)
    #do stuff here to capitalize the string
    return outStr

    foo = capitalize("bar ")

    hope this helps.

    Comment

    • Laszlo Zsolt Nagy

      #3
      Re: Difference between a library and a module...

      sophie_newbie wrote:
      [color=blue]
      >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?
      >
      >[/color]
      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...)
      [color=blue]
      >And if i do string.replace( ) am I using a module or a function or a
      >method or what?
      >
      >[/color]
      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.




      I hope this helps.

      Laszlo

      Comment

      • bruno at modulix

        #4
        Re: Difference between a library and a module...

        sophie_newbie wrote:[color=blue]
        > OK this might seem like a retarded question,[/color]

        Better to look like an ignorant than to stay one !-)
        [color=blue]
        > but what is the difference
        > between a library and a module?[/color]

        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...
        [color=blue]
        > If I do:
        >
        > import string
        >
        > am I importing a module or a library?[/color]

        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 !-)
        [color=blue]
        > And if i do string.replace( ) am I using a module or a function or a
        > method or what?[/color]

        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,... ).
        [color=blue]
        > Sorry.[/color]

        Why ?

        HTH
        --
        bruno desthuilliers
        python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
        p in 'onurb@xiludom. gro'.split('@')])"

        Comment

        • Bruno Desthuilliers

          #5
          Re: Difference between a library and a module...

          Laszlo Zsolt Nagy a écrit :[color=blue]
          > sophie_newbie wrote:
          >[color=green]
          >> 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?
          >>
          >>[/color]
          > I'm not a guru, but... I think that modules are things that live inside
          > the Python language.[/color]

          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)[color=blue]
          > 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...)[/color]

          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.

          [color=blue]
          > 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,[/color]

          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.[color=blue][color=green][color=darkred]
          >>> import deco
          >>> deco[/color][/color][/color]
          <module 'deco' from 'deco.py'>[color=blue][color=green][color=darkred]
          >>> deco.__class__[/color][/color][/color]
          <type 'module'>[color=blue][color=green][color=darkred]
          >>> deco.__class__. __name__[/color][/color][/color]
          'module'

          Seems like they do have one...
          [color=blue]
          > and they cannot be instantiated[color=green][color=darkred]
          >>> deco.__class__( 'foo')[/color][/color][/color]
          <module 'foo' (built-in)>[color=blue][color=green][color=darkred]
          >>> import types
          >>> types.ModuleTyp e('bar')[/color][/color][/color]
          <module 'bar' (built-in)>
          [color=blue]
          >
          > I hope this helps.
          >[/color]
          So do I !-)

          Comment

          • Bruno Desthuilliers

            #6
            Re: Difference between a library and a module...

            akameswaran@gma il.com a écrit :[color=blue]
            > I'm not 100% sure what is a library in python.[/color]

            Technically, nothing.
            [color=blue]
            >
            > string.replace( ) I'm 90% sure is a function in the string module.[/color]
            it is.
            [color=blue]
            > However something like this:
            > foo = "bar"
            > foo.Capitalize( )[/color]

            s/C/c/
            [color=blue]
            > bar.capitalize is a method.[/color]

            ....which is usually built from a function.
            [color=blue]
            >
            > 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.[/color]

            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.
            [color=blue]
            > 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[/color]
            outStr = inStr[0].upper() + intStr[1:].lower()[color=blue]
            > return outStr
            >
            > foo = capitalize("bar ")[/color]

            Defining a method is really just defining a function:
            [color=blue][color=green][color=darkred]
            >>> class StringWrapper(s tr): pass[/color][/color][/color]
            ....[color=blue][color=green][color=darkred]
            >>> s = StringWrapper(' foo')
            >>> s[/color][/color][/color]
            'foo'[color=blue][color=green][color=darkred]
            >>> def capitalize(s):[/color][/color][/color]
            .... 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)
            ....[color=blue][color=green][color=darkred]
            >>> StringWrapper.c apitalize = capitalize
            >>> s.capitalize()[/color][/color][/color]
            yaoo, I was just a function, I'll be promoted to a method
            'Foo'[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            Comment

            Working...