Attributes

Definitions such as f[x_]=x^2 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 (RefLink[Flat, paclet:ref/Flat]) to specify that a particular function is "flat", so that nested invocations are automatically flattened, and it behaves as if it were associative.

This assigns the attribute (RefLink[Flat, paclet:ref/Flat]) to the function f:

SetAttributes[f,Flat]

Now f behaves as a flat, or associative, function, so that nested invocations are automatically flattened:

f[f[a,b],c]

Attributes like (RefLink[Flat, paclet:ref/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.

Here is a definition for the flat function f:

f[x_,x_]:=f[x]

Because f is flat, the definition is automatically applied to every subsequence of arguments:

f[a,a,a,b,b,b,c,c]

Manipulating attributes of symbols.

This shows the attributes assigned to f:

Attributes[f]

This removes the attributes assigned to f:

Attributes[f]={}

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

Here are the attributes for the built-in function (RefLink[Plus, paclet:ref/Plus]):

Attributes[Plus]

An important attribute assigned to built-in mathematical functions in the Wolfram Language is the attribute (RefLink[Listable, paclet:ref/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.

The built-in (RefLink[Log, paclet:ref/Log]) function is (RefLink[Listable, paclet:ref/Listable]):

Log[{5,8,11}]

This defines the function p to be listable:

SetAttributes[p,Listable]

Now p is automatically threaded over lists that appear as its arguments:

p[{a,b,c},d]

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 (RefLink[OneIdentity, paclet:ref/OneIdentity]) affects only pattern matching, as discussed in "Flat and Orderless Functions". Similarly, the attribute (RefLink[Constant, paclet:ref/Constant]) is only relevant in differentiation, and operations that rely on differentiation.

The (RefLink[Protected, paclet:ref/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 (RefLink[Protect, paclet:ref/Protect]) and (RefLink[Unprotect, paclet:ref/Unprotect]) discussed in "Modifying Built-in Functions" can be used as alternatives to (RefLink[SetAttributes, paclet:ref/SetAttributes]) and (RefLink[ClearAttributes, paclet:ref/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.

Here is a definition for the function g:

g[x_]=x+1

This sets the (RefLink[Protected, paclet:ref/Protected]) attribute for g:

Protect[g]

Now you cannot modify the definition of g:

g[x_]=x

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 (RefLink[ReadProtected, paclet:ref/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.

Although you cannot modify it, you can still look at the definition of g:

?g

This sets the (RefLink[ReadProtected, paclet:ref/ReadProtected]) attribute for g:

SetAttributes[g,ReadProtected]

Now you can no longer read the definition of g:

?g

Functions like (RefLink[SetAttributes, paclet:ref/SetAttributes]) and (RefLink[ClearAttributes, paclet:ref/ClearAttributes]) usually allow you to modify the attributes of a symbol in any way. However, if you once set the (RefLink[Locked, paclet:ref/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 (RefLink[Locked, paclet:ref/Locked]) attribute in addition to (RefLink[Protected, paclet:ref/Protected]) or (RefLink[ReadProtected, paclet:ref/ReadProtected]), you can arrange for it to be impossible for users to modify or read definitions.

Clearing values and attributes.

This clears values and attributes of p which was given attribute (RefLink[Listable, paclet:ref/Listable]) above:

ClearAll[p]

Now p is no longer listable:

p[{a,b,c},d]

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.

By explicitly calling (RefLink[Thread, paclet:ref/Thread]), you can implement the transformation that would be done automatically if p were listable:

Thread[p[{a,b,c},d]]

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.

Pure functions with attributes.

This pure function applies p to the whole list:

Function[{x},p[x]][{a,b,c}]

By adding the attribute (RefLink[Listable, paclet:ref/Listable]), the function gets distributed over the elements of the list before applying p:

Function[{x},p[x],{Listable}][{a,b,c}]