astropy/astropy — pull request #1201 by astrofrog (2013-09-20 01:08:25+00:00)
Title: Tweaks to make `Unit` and `Quantity` interfaces more similar

I've been using astropy's units module today (which is awesome).

As far as I can tell, the easiest way to determine the units of something which could be either a unit or a quantity is:
```
u = (1 * x).unit
```
Which is a slightly non-obvious type cast. It would be handy if Unit had a `.unit` attribute (a property which returns `self`) to match `Quantity.unit`. Also handy (but maybe slightly more controversial) would be a `.value` property that returns 1.

These tweaks would make it easier to write code that's agnostic about Quantities vs Units

I've also been using the following utility function:
```
def split(x):
if isinstance(1 * x, Quantity)
return x.unit, x.value
return 1, x

u, v = split(x)
return u * np.sin(v)
```
This unpacks and repacks a Unit/Quantity to avoid the implicit conversion warning. Importantly (and unlike `decompose`), it also works if x is just a number or array.

It might be nice to include a function like split in the units module and docs, unless you recommend a different approach (perhaps an `asquantity` method to match `np.asarray`?)