Class Not Auto-Init On Import

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Apr 21 13:06:19 CEST 2007


En Sat, 21 Apr 2007 05:46:07 -0300, Robert Rawlins - Think Blue  
<robert.rawlins at thinkbluemedia.co.uk> escribió:

>> From my understanding of what I've read, the 'import' is meant to auto  
>> Init
> my class ready for me to access its methods, but it doesn't appear too,  
> I'm
> having to init them myself before I can access them, like this.

The import statement does not "auto Init" anything, in principle.

> import LocationService

Here, you import the LocationService module. If it is imported by the  
first time, its code (at the module level) is executed. The imported  
module is available under the name "LocationService" in the current  
namespace.

> Location = LocationService.LocationService()

Presumably, the module LocationService defines a class of the same name;  
you refer to that class using LocationService.LocationService. The last ()  
create an instance of that class; that instance is bound to the name  
Location.

> LocationService.setIP('192.168.1.1')

This may or may not be correct; here you call a (global) function setIP  
inside the LocationService module.

> Why is this the case? Should i not just be able to access the setIP()  
> method
> by doing LocationService.setIP('192.168.1.1') Without having to create my
> own inited reference?

Not enough info, without knowing how LocationService is supposed to be  
used. But as I said, the setIP call looks suspicious.

-- 
Gabriel Genellina




More information about the Python-list mailing list