d-coder
d-coder

Reputation: 13963

Use of python keyword "as" in the example

I came across the code which goes like the following:

import redis
import redis as originalRedis

OriginalRedis = originalRedis.Redis          # note the uppercase O 
class Redis(redis.Redis):

    def keys(self, pattern='*'):
        # custom implementation to be done here
        client = OriginalRedis(self.host, port=self.port, db=self.db)
        return client.keys(pattern)

    def flushKeys(self):
        # some custom implementation TBD here too
        return True

redis.Redis = Redis

Now my question is specifically on the imports statements. When we import something like that do we two references to the same module ? Aren't module imports supposed to be singleton objects ? And I've tried the following

import datetime
import datetime as dt 
dt is datetime 
True

So I'm guessing there is one reference of a single module as id(dt) and id(datetime) are same. Could anyone elaborate on why this has been used in this way ? Thanks in advance!

Upvotes: 4

Views: 5969

Answers (2)

d-coder
d-coder

Reputation: 13963

I think the OP is confused between instances and references!

When we import something like that do we've two references to the same module?

import datetime
import datetime as dt 

Yes. But you should also learn that objects/instances are different from references (Obviously you know this by now!). I think the OP thinks that multiple imports will kind of del the previous reference to the module (hence making previous imports stale) and create a new one which isn't the case! See the code below for yourself and make sure of this.

import datetime          # first import
import datetime as dt    # second import with an alias
id(dt)                  
X                        # X stands for some random address
id(datetime)
X                        # same address as previous
import datetime          # here OP thinks it'll exhaust the previous instance
id(datetime) 
X                        # to OP's surprise it remains the same! 

Aren't module imports supposed to be singleton objects?

Yes. And no language prohibits you from creating as many references as you want to the same singleton objects!

NOTE: Some OP's are just humans like me or even me!

Thank you all!

Upvotes: 1

Chris
Chris

Reputation: 22953

When we import something like that do we two references to the same module?

Correct. All the as clause does is bind a new name to an object. If you import a module directly, and then import the same module but with a different name via as, both names will point to the exact same object. eg:

>>> import re
>>> import re as regex
>>> 
>>> id(re) == id(regex)
True

>>>

The documentation for import statements also mentions this behavior:

If the module name is followed by as, then the name following as is bound directly to the imported module.


Aren't module imports supposed to be singleton objects?

Yes, you can think of them like that. Python only executes the code for an imported module as needed, so technically yes. Module objects are singletons. But you must remember, even singletons can have multiple names.

Just because you created a new alias for an object, doesn't mean you created an entirely new object. All you did is simply create a new name to refer to the exact same object. If this wasn't true, then the code above would have returned False as re and regex would have been different objects,

Upvotes: 4

Related Questions