Unit class

(Shortest import: from brian2 import Unit)

class brian2.units.fundamentalunits.Unit(value, dim=None, scale=None)[source]

Bases: brian2.units.fundamentalunits.Quantity

A physical unit.

Normally, you do not need to worry about the implementation of units. They are derived from the Quantity object with some additional information (name and string representation).

Basically, a unit is just a number with given dimensions, e.g. mvolt = 0.001 with the dimensions of voltage. The units module defines a large number of standard units, and you can also define your own (see below).

The unit class also keeps track of various things that were used to define it so as to generate a nice string representation of it. See below.

When creating scaled units, you can use the following prefixes:

Factor Name Prefix
10^24 yotta Y
10^21 zetta Z
10^18 exa E
10^15 peta P
10^12 tera T
10^9 giga G
10^6 mega M
10^3 kilo k
10^2 hecto h
10^1 deka da
10^-1 deci d
10^-2 centi c
10^-3 milli m
10^-6 micro u (mu in SI)
10^-9 nano n
10^-12 pico p
10^-15 femto f
10^-18 atto a
10^-21 zepto z
10^-24 yocto y

Defining your own

It can be useful to define your own units for printing purposes. So for example, to define the newton metre, you write >>> from brian2.units.allunits import metre, newton >>> Nm = newton * metre

You can then do

>>> (1*Nm).in_unit(Nm)
'1. N m'

which returns "1 N m" because the Unit class generates a new display name of "N m" from the display names "N" and "m" for newtons and metres automatically.

To register this unit for use in the automatic printing of the Quantity.in_best_unit() method, see the documentation for the UnitRegistry class.


The best way to construct a new unit is to use standard units already defined and arithmetic operations, e.g. newton*metre. See the documentation for the static methods Unit.create and Unit.create_scaled_units for more details.

If you don’t like the automatically generated display name for the unit, use the Unit.set_display_name() method.


A new unit defined by multiplication, division or taking powers generates a name for the unit automatically, so that for example the name for pfarad/mmetre**2 is "pF/mm^2", etc. If you don’t like the automatically generated name, use the Unit.set_display_name() method.


dim The Dimensions of this unit


create(dim[, name, dispname, latexname, ...]) Create a new named unit.



The Dimensions of this unit

static create(dim, name='', dispname='', latexname=None, scalefactor='', **keywords)[source]

Create a new named unit.


dim : Dimension

The dimensions of the unit.

name : str, optional

The full name of the unit, e.g. 'volt'

dispname : str, optional

The display name, e.g. 'V'

latexname : str, optional

The name as a LaTeX expression (math mode is assumed, do not add $ signs or similar), e.g. '\omega'. If no latexname is specified, dispname will be used.

scalefactor : str, optional

The scaling factor, e.g. 'm' for mvolt

keywords :

The scaling for each SI dimension, e.g. length="m", mass="-1", etc.


u : Unit

The new unit.