e01.org

Scope resolution operator

In computer programming, scope is an enclosing context where values and expressions are associated. The scope resolution operator helps to identify and specify the context to which an identifier refers, particularly by specifying a namespace or class. The specific uses vary across different programming languages with the notions of scoping. In many languages, the scope resolution operator is written :: . In some languages, notably those influenced by Modula-3 (including Python and Go), modules are objects, and scope resolution within modules is a special case of usual object member access, so the usual method operator . is used for scope resolution. Other languages, notably C++ and Ruby, feature both scope resolution and method access, which interact in various ways; see examples below.

In computer programming, scope is an enclosing context where values and expressions are associated. The scope resolution operator helps to identify and specify the context to which an identifier refers, particularly by specifying a namespace or class. The specific uses vary across different programming languages with the notions of scoping. In many languages, the scope resolution operator is written :: . In some languages, notably those influenced by Modula-3 (including Python and Go), modules are objects, and scope resolution within modules is a special case of usual object member access, so the usual method operator . is used for scope resolution. Other languages, notably C++ and Ruby, feature both scope resolution and method access, which interact in various ways; see examples below. class A { public: static int i; // scope of i is A }; namespace B { int c = 2; } // namespace B int A::i = 4; // scope operator refers to the integer i declared in the class A int x = B::c; // scope operator refers to the integer c declared in the namespace B $ php -r :: Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) As of PHP 8, the Hebrew name has been removed from the error message: $ php -r :: Parse error: syntax error, unexpected token "::", expecting end of file in Command line code on line 1 module Example Version = 1.0 class "Hello world" Example.hello "hacker" # => "Hello hacker" Example::Version # => 1.0 Example.Version # NoMethodError This illustrates the difference between the message (.) operator and the scope operator in Ruby (::) We can use both ::hello and .hello, because hello is a part of Example's scope and because Example responds to the message hello. We can't do the same with ::Version and .Version, because Version is within the scope of Example, but Example can't respond to the message Version, since there is no method to respond with. Scope is also affected by sigils which preface variable names: " $ " - global variable " @ " - instance variable of self " @@ " - class variable No sigil, lowercase or underscore - local variable or method No sigil, uppercase - constant Python does not have a dedicated scope resolution operator. Name lookup follows the LEGB rule (Local, Enclosed, Global, Built-in). Access to attributes of modules, classes, and objects is performed using the dot (.) operator. import math x = 5 # global variable def outer(): x = 10 # enclosing scope def inner(): print(x) # resolves to the enclosing scope inner() outer() print(math.pi) # attribute access using the dot operator

References

  1. ^ "WTF Is T_paamayim_nekudotayim (2013) | Hacker News" . 2025-01-30. Archived from the original on 30 January 2025 . Retrieved 2025-05-17 . Wikipedia revision 1343574744, retrieved 2026-11-19 · 1747436400.0
  2. ^ "WTF Is T_paamayim_nekudotayim (2013) | Hacker News" . news.ycombinator.com . Archived from the original on 2025-01-30 . Retrieved 2025-05-17 . Wikipedia revision 1343574744, retrieved 2026-11-19 · 1747436400.0
  3. ^ smog_alado (2021-04-24). "[PHP] T_PAAMAYIM_NEKUDOTAYIM, a tale of nerd gridlock" . r/HobbyDrama . Retrieved 2025-05-17 . Wikipedia revision 1343574744, retrieved 2026-11-19 · 1747436400.0
  4. ^ "Scope Resolution Operator" . PHP 5 Manual . Retrieved 2007-08-09 . Wikipedia revision 1343574744, retrieved 2026-11-19 · 1186614000.0
  5. ^ "PHP: rfc:rename-double-colon-token" . wiki.php.net . Retrieved 2025-05-17 . Wikipedia revision 1343574744, retrieved 2026-11-19 · 1747436400.0
  6. ^ "2T8qQ - created on 3v4l.org" . 3v4l.org . 2013-09-10 . Retrieved 2025-05-17 . Wikipedia revision 1343574744, retrieved 2026-11-19 · 1747436400.0
  7. ^ "4. Execution model" . Python documentation . Retrieved 2026-03-15 . Wikipedia revision 1343574744, retrieved 2026-11-19 · 1773529200.0
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Scope resolution operator". 1773544695. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1343574744

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·