splitting tables

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Wed Feb 11 20:16:42 EST 2004


On Thu, 12 Feb 2004 00:56:22 +0000 (UTC), robsom wrote:
>> On Wed, 11 Feb 2004 00:44:18 +0000 (UTC), robsom wrote:
>>> [why do str.split() and split(str) both work?]
>> Because somewhere earlier in that code you must have imported the name
>> 'split' into the base namespace.  It's not there to begin with (and
>> importing it is needlessly polluting the base namespace).
>
> in fact there is an import string * instruction at the beginning.

Yes, the "from module import *" is a deprecated usage; it pollutes the
base namespace, which leads to confusions like this and others.

The recommended way is to:

    import module

    module.function()

So, in your case, this would be:

    import string

    string.split( str )

Then it becomes clear in each instance that the "split" function comes
from the "string" module, and not some other arbitrary module.

>>> [are they the same function?]
>> They're two different functions.  One is a method of string objects,
>> one is a function you've imported from somewhere.  (This confusion is
>> partly why importing symbols into the base namespace is a bad idea.)
>
> ok, so you suggest using the method because it results in a faster
> code?

No, I recommend using the method because it's already part of the string
objects, and results in clearer code.

-- 
 \           "It ain't so much the things we don't know that get us in |
  `\      trouble. It's the things we know that ain't so."  -- Artemus |
_o__)                                  Ward (1834-67), U.S. journalist |
Ben Finney <http://bignose.squidly.org/>



More information about the Python-list mailing list