Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on building a simple python program for a class which will run on a Raspberry Pi and an Arduino to direct a telescope. I had started to learn python some time ago, and I'm having trouble getting my functions to work properly. Right now, I have this:

import ephem

def const(p, d): # find the constellation #
    def loc():
        sbend = ephem.Observer()
        sbend.lat = '41.67'
        sbend.lon = '86.26'
        p = getattr(ephem, p)
        p.compute(sbend)
        print p.alt, p.az

    o = getattr(ephem, p)
    print ephem.constellation(o(d))
    return loc()

const(raw_input('Planet: '), raw_input('yyyy/mm/dd: '))

From what I remember, a function inside another can call a variable from the parent. Can it also work the other way round like I have at the end? I'd like to be able to print the constellation (which is working) as well as the alt and az of the planet based on the hardcoded location. For some reason, it isn't calculating the altitude and azimuth though. Thoughts?

EDIT

I added return loc() on line 14.

I was doing more reading and some other threads said that to get to an inner function, it needs to be returned at the end of the parent. But, it's still not working for me.

share|improve this question
 
I see that you have defined a function named loc() but I cannot see that you use it anywhere. So before I go further: is there any code that you accidentally omitted when you pasted this code in? –  Brandon Rhodes Oct 9 at 12:07
 
Sorry, I just added it to line 14. –  Brian Bennett Oct 9 at 13:10
 
If you want to return the function, you just say return loc — what you are doing instead, by saying return loc(), is you are first calling loc() yourself to get a return value (which will be None, since loc() includes no return statement), then you are returning that None value as the return value of const(). –  Brandon Rhodes Oct 9 at 13:24
add comment

2 Answers

up vote 1 down vote accepted

I am not clear on why you have one function inside of another, so I might be missing part of the problem that you are trying to solve; but if I wanted to determine in what constellation a planet lies, where the planet's name and the date are provided as inputs, then I would simply perform those steps all in a row, without any complicated functions-inside-of-functions:

import ephem

def const(planet_name, date_string):
    planet_class = getattr(ephem, planet_name)
    planet = planet_class()
    south_bend = ephem.Observer()
    south_bend.lat = '41.67'
    south_bend.lon = '-86.26'  # west is negative
    south_bend.date = date_string
    planet.compute(south_bend)
    return ephem.constellation((planet.ra, planet.dec))

print const(raw_input('Planet: '), raw_input('yyyy/mm/dd: '))
share|improve this answer
 
I have two questions: why the double parentheses in return ephem.constellation((planet.ra, planet.dec))? Second, it's still not wanting to return any position data after choosing a planet and setting the date. –  Brian Bennett Oct 9 at 14:15
 
You can ignore that last question. –  Brian Bennett Oct 9 at 14:30
 
The inner set of parenthesis are creating a tuple. I could also have written that in two steps: tup = (planet.ra, planet.dec) and then said ephem.constellation(tup) –  Brandon Rhodes Oct 9 at 16:38
add comment

Taking @Brandon's example, here is my final code:

import ephem

def const(planet_name, date_string): 
    planet_class = getattr(ephem, planet_name) 
    planet = planet_class() 
    south_bend = ephem.Observer()
    south_bend.lat = '41.67'
    south_bend.lon = '-86.26'
    south_bend.date = date_string              
    planet.compute(south_bend)                  
    print ephem.constellation((planet.ra, planet.dec))
    return planet.alt, planet.az

print const(raw_input('Planet: '), raw_input('yyyy/mm/dd: '))

I realized after looking at it that he was using planet.ra and planet.dec to locate the background constellation, not to print it's coordinates in the sky. All I did was add the return call at the end to print the altitude and azimuth data.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.