Dalekmun2010
Posts: 10
Joined: Sun Feb 19, 2012 5:18 am
Contact: Website

Python n00b, I need a basic explanation of "of".

Mon Aug 27, 2012 11:27 am

Hi! I'm not sure if this is the right board to post this to, If not, feel free to move this to an appropriate board.
Anyway, I have bought some stuff for my Pi's GPIO, and have decided to learn Python.
When going over a few example codes there where a few things I couldn't find answers to in the Python books I've been reading/ On the net.
The line that has been confusing me is

Code: Select all

import RPi.GPIO as GPIO
I figured that "import" imports a module, yet haven't been able to work out what "as"does.
If anyone would be so kind as to explain this to me, I would be forever grateful.
Yours truly, Ryan Mitchell.
Because I'm a potato!

rasbeer
Posts: 242
Joined: Wed Mar 07, 2012 8:35 am

Re: Python n00b, I need a basic explanation of "of".

Mon Aug 27, 2012 11:36 am

It lets the code in the module refer to the RPi.GPIO as just GPIO, instead of having to call it RPi.GPIO all the time.

(I think this is called importing it into the 'local namespace'.)

timhoffman
Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: Python n00b, I need a basic explanation of "of".

Mon Aug 27, 2012 11:58 am

There is a PEP (Python Enhancement Proposals) for the introduction of "import somemodule as somename" which provides info on the rationale for introducing it. http://www.python.org/dev/peps/pep-0221/

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python n00b, I need a basic explanation of "of".

Mon Aug 27, 2012 12:37 pm

BTW the import looks a bit strange to me because ``as`` is usually there for *renaming* an imported name. Here GPIO is ”renamed” to GPIO which does not make much sense. Just importing names from another module into the current modules namespace is done with ``from … import …``:

Code: Select all

from RPi import GPIO

Code: Select all

while not self.asleep():
    sheep += 1

Dalekmun2010
Posts: 10
Joined: Sun Feb 19, 2012 5:18 am
Contact: Website

Re: Python n00b, I need a basic explanation of "of".

Tue Aug 28, 2012 11:09 am

Thanks everyone for replying! I really appreciate you all taking the time to help me. And again, Thanks!
Because I'm a potato!

Return to “Python”