Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Circular reference
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==In computer programming== {{For|circular references between objects or resources|Reference counting}} Circular references can appear in [[computer programming]] when one piece of code requires the result from another, but that code needs the result from the first. For example, the two functions, posn and plus1 in the following Python program comprise a circular reference:{{explain|date=April 2020}} <syntaxhighlight lang="python"> def posn(k: int) -> int: if k < 0: return plus1(k) return k def plus1(n: int) -> int: return posn(n + 1) </syntaxhighlight> Circular references like the above example may return valid results if they have a terminating condition. If there is no terminating condition, a circular reference leads to a condition known as [[livelock]] or [[infinite loop]], meaning it theoretically could run forever. <syntaxhighlight lang="python"> def posn(k: int) -> int: return plus1(k) def plus1(n: int) -> int: return posn(n + 1) </syntaxhighlight> In ISO Standard, SQL circular integrity constraints are implicitly supported within a single table. Between multiple tables circular constraints (e.g. foreign keys) are permitted by defining the constraints as deferrable (See [http://www.postgresql.org/docs/current/static/sql-createtable.html CREATE TABLE] for PostgreSQL and [http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm#i1015767 DEFERRABLE Constraint Examples] for Oracle). In that case the constraint is checked at the end of the transaction not at the time the DML statement is executed. To update a circular reference, two statements can be issued in a single transaction that will satisfy both references once the transaction is committed. Circular references can also happen between instances of data of a mutable type, such as in this Python script: <syntaxhighlight lang="python"> mydict = { "this": "that", "these": "those" } mydict["myself"] = mydict print(mydict) </syntaxhighlight> The {{code|lang=python|print(mydict)}} function will output {{code|lang=python|{'this': 'that', 'these': 'those', 'myself': {...}<nowiki>}</nowiki>}}, where {{code|lang=python|{...}<nowiki/>}} indicates a circular reference, in this case, to the {{code|lang=python|mydict}} dictionary.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)