Skip to main content

symbol-package

symbol-package Function

Syntax:

symbol-package symbol → contents

Arguments and Values:

symbol—a symbol.

contents—a package object or nil.

Description:

Returns the home package of symbol.

Examples:

(in-package "CL-USER") → #<PACKAGE "COMMON-LISP-USER"> 
(symbol-package ’car) → #<PACKAGE "COMMON-LISP">
(symbol-package ’bus) → #<PACKAGE "COMMON-LISP-USER">
(symbol-package :optional) → #<PACKAGE "KEYWORD">
;; Gensyms are uninterned, so have no home package.
(symbol-package (gensym)) → NIL
(make-package ’pk1) → #<PACKAGE "PK1">
(intern "SAMPLE1" "PK1") → PK1::SAMPLE1, NIL
(export (find-symbol "SAMPLE1" "PK1") "PK1") → T
(make-package ’pk2 :use(pk1)) → #<PACKAGE "PK2">
(find-symbol "SAMPLE1" "PK2") → PK1:SAMPLE1, :INHERITED
(symbol-package ’pk1::sample1) → #<PACKAGE "PK1">
(symbol-package ’pk2::sample1) → #<PACKAGE "PK1">
(symbol-package ’pk1::sample2) → #<PACKAGE "PK1">
(symbol-package ’pk2::sample2) → #<PACKAGE "PK2">
;; The next several forms create a scenario in which a symbol
;; is not really uninterned, but is "apparently uninterned",
;; and so SYMBOL-PACKAGE still returns NIL.
(setq s3 ’pk1::sample3) → PK1::SAMPLE3
(import s3 ’pk2) → T
(unintern s3 ’pk1) → T
(symbol-package s3) → NIL
(eq s3 ’pk2::sample3) → T

Affected By:

import, intern, unintern

Exceptional Situations:

Should signal an error of type type-error if symbol is not a symbol.

See Also:

intern

Expanded Reference: symbol-package

Basic usage

symbol-package returns the home package of a symbol, which is the package where it was first interned.

(symbol-package 'car)
==> #<PACKAGE "COMMON-LISP">
(symbol-package 'list)
==> #<PACKAGE "COMMON-LISP">

User-defined symbols

Symbols created in user code typically have COMMON-LISP-USER as their home package.

(defvar *my-var* 42)
(symbol-package '*my-var*)
==> #<PACKAGE "COMMON-LISP-USER">

Keywords

Keywords live in the KEYWORD package.

(symbol-package :test)
==> #<PACKAGE "KEYWORD">
(symbol-package :hello)
==> #<PACKAGE "KEYWORD">

Uninterned symbols have no home package

Symbols created with make-symbol or gensym are not interned in any package, so symbol-package returns NIL.

(symbol-package (make-symbol "TEMP"))
=> NIL
(symbol-package (gensym))
=> NIL

Symbols in custom packages

When you intern a symbol in a custom package, that package becomes its home package.

(defpackage "MY-PKG" (:use))
(intern "MY-SYM" "MY-PKG")
(symbol-package (find-symbol "MY-SYM" "MY-PKG"))
==> #<PACKAGE "MY-PKG">