nils wagenaar wrote:
> Hello,
>
>
> Could i use a variable defined in a function in another function?
>
> I have now:
>
>
> def DatasetToSubset(file, LatUpbound, LatLowBound, LonUpBound,
> LonLowBound):
> nc=netCDF4.Dataset(file)
> lats=nc.variables['lat'][:]; lons=nc.variables['lon'][:]
> latselect=np.logical_and(lats > LatLowBound, lats < LatUpBound)
> lonselect=np.logical_and(lon > LonLowBound, lon < LonUpBound)
> data=nc.variables['Runoff'][1000, latselect, lonselect]
> return data; return latselect; return lonselect
It doesn't help that you put all return statements on the same line, only
the first
return data
is executed; the other two are unreachable code.
> So, i want to use latselect and lonselect in a different function where i
> interpolate for the subsetted area.
In Python while you can return only one value you can easily combine
multiple values into one tuple. Instead of
> return data; return latselect; return lonselect
write
return data, latselect, lonselect
When you call the function you can either access the parts of the tuple with
their respective index
result = DatasetToSubset(...)
lat = result[1]
or use a language feature called "unpacking" to break the tuple into the
individual values:
data, lat, lon = DatasetToSubset(...)
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor