question about shadowing built-in names

J

John Salerno

I understand that if you reassign a built-in name to your own variable,
such as:

str = 'hello'

then you lose the use of the built-in (in this case str()), but is this
also the case in terms of imported names? For example:

import MySQLdb

db = connect(blah blooh blee)
cursor = db.cursor()

Now, in the above, did I reassign the value of the cursor method, or is
this different?

Actually, now that I think about it, that's not exactly the same because
cursor is a method of a connection object, not simply a function. So I
suppose my real question is, does the 'shadowing' of built-in names also
happen when you import other names, whether they are functions,
methods or whatever else they could be?

Thanks.
 
D

Dennis Lee Bieber

I understand that if you reassign a built-in name to your own variable,
such as:

str = 'hello'

then you lose the use of the built-in (in this case str()), but is this
also the case in terms of imported names? For example:

import MySQLdb

db = connect(blah blooh blee)

Actually, now that I think about it, that's not exactly the same because
cursor is a method of a connection object, not simply a function. So I
suppose my real question is, does the 'shadowing' of built-in names also
happen when you import other names, whether they are functions,
methods or whatever else they could be?
Well, if you have a habit of doing
from module import *
and later rebind one of the imported names...

And that is also why the from/import * is frowned upon...

mod_one
-=-=-=-=-=-
x = "mod_two" #yes, I'm being evil

mod_two
-=-=-=-=-=-
x = "mod_one"

main1
-=-=-=-=-=-
from mod_one import *
from mod_two import *

print x

main2
-=-=-=-=-=-
from mod_two import *
from mod_one import *

print x

The output of main1 will be: mod_one (which is actually in mod_two),
and the other alternative from main2. Using from/import * on modules
that you do not have full cognizance of the internals can change results
based on the order of the imports.

And for real viciousness, consider what a from/import * does for
/this/ module

mod_three
-=-=-=-=-=-

str = list
list = dict

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Salerno

Dennis said:
Actually, the above has already failed <G> Should be
MySQLdb.connect...

Heh heh, yeah, I totally meant to do that on purpose. ;)

Well, if you have a habit of doing
from module import *
and later rebind one of the imported names...

I see. So doing a regular import <name> statement keeps the namespace
confined to that module, right? So I would be able to create my own
variable with the same name as a name in the imported module, and this
wouldn't be a problem as long as I didn't import with *?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top