# Attributes

Definitions such as specify *values* for functions. Sometimes, however, you need to specify general properties of functions, without necessarily giving explicit values.

The Wolfram Language provides a selection of *attributes* that you can use to specify various properties of functions. For example, you can use the attribute Flat to specify that a particular function is "flat", so that nested invocations are automatically flattened, and it behaves as if it were associative.

In[1]:= |

In[2]:= |

Out[2]= |

Attributes like Flat can affect not only evaluation, but also operations such as pattern matching. If you give definitions or transformation rules for a function, you must be sure to have specified the attributes of the function first.

In[3]:= |

In[4]:= |

Out[4]= |

Attributes[f] | give the attributes of f |

Attributes[f]={attr_{1},attr_{2},…} | set the attributes of f |

Attributes[f]={} | set f to have no attributes |

SetAttributes[f,attr] | add attr to the attributes of f |

ClearAttributes[f,attr] | remove attr from the attributes of f |

Manipulating attributes of symbols.

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

Orderless | orderless, commutative function (arguments are sorted into standard order) |

Flat | flat, associative function (arguments are "flattened out") |

OneIdentity | , etc. are equivalent to a for pattern matching |

Listable | f is automatically "threaded" over lists that appear as arguments (e.g., becomes ) |

Constant | all derivatives of f are zero |

NumericFunction | f is assumed to have a numerical value when its arguments are numeric quantities |

Protected | values of f cannot be changed |

Locked | attributes of f cannot be changed |

ReadProtected | values of f cannot be read |

HoldFirst | the first argument of f is not evaluated |

HoldRest | all but the first argument of f are not evaluated |

HoldAll | none of the arguments of f are evaluated |

HoldAllComplete | the arguments of f are treated as completely inert |

NHoldFirst | the first argument of f is not affected by N |

NHoldRest | all but the first argument of f are not affected by N |

NHoldAll | none of the arguments of f are affected by N |

SequenceHold | Sequence objects appearing in the arguments of f are not flattened out |

Temporary | f is a local variable, removed when no longer used |

Stub | Needs is automatically called if f is ever explicitly input |

The complete list of attributes for symbols in the Wolfram Language.

In[7]:= |

Out[7]= |

An important attribute assigned to built‐in mathematical functions in the Wolfram Language is the attribute Listable. This attribute specifies that a function should automatically be distributed or "threaded" over lists that appear as its arguments. This means that the function effectively gets applied separately to each element in any lists that appear as its arguments.

In[8]:= |

Out[8]= |

In[9]:= |

In[10]:= |

Out[10]= |

Many of the attributes you can assign to functions in the Wolfram Language directly affect the evaluation of those functions. Some attributes, however, affect only other aspects of the treatment of functions. For example, the attribute OneIdentity affects only pattern matching, as discussed in "Flat and Orderless Functions". Similarly, the attribute Constant is only relevant in differentiation, and operations that rely on differentiation.

The Protected attribute affects assignments. The Wolfram Language does not allow you to make any definition associated with a symbol that carries this attribute. The functions Protect and Unprotect discussed in "Modifying Built-in Functions" can be used as alternatives to SetAttributes and ClearAttributes to set and clear this attribute. As discussed in "Modifying Built-in Functions", most built‐in Wolfram Language objects are initially protected so that you do not make definitions for them by mistake.

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

You can usually see the definitions you have made for a particular symbol by typing ?f, or by using a variety of built‐in Wolfram Language functions. However, if you set the attribute ReadProtected, the Wolfram Language will not allow you to look at the definition of a particular symbol. It will nevertheless continue to use the definitions in performing evaluation.

In[15]:= |

Functions like SetAttributes and ClearAttributes usually allow you to modify the attributes of a symbol in any way. However, if you once set the Locked attribute on a symbol, then the Wolfram Language will not allow you to modify the attributes of that symbol for the remainder of your Wolfram System session. Using the Locked attribute in addition to Protected or ReadProtected, you can arrange for it to be impossible for users to modify or read definitions.

Clearing values and attributes.

In[17]:= |

In[18]:= |

Out[18]= |

By defining attributes for a function you specify properties that the Wolfram Language should assume whenever that function appears. Often, however, you want to assume the properties only in a particular instance. In such cases, you will be better off not to use attributes, but instead to call a particular function to implement the transformation associated with the attributes.

In[19]:= |

Out[19]= |

Functions that perform transformations associated with some attributes.

Attributes in the Wolfram Language can only be permanently defined for single symbols. However, the Wolfram Language also allows you to set up pure functions which behave as if they carry attributes.

Function[vars,body,{attr_{1},…}] | a pure function with attributes , … |

Pure functions with attributes.

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |