Attribute (programming)
≈ 6 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
, classes can contain attributes and methods. can be represented as a column or field. In object-oriented programming, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such. For clarity, attributes should more correctly be considered metadata. An attribute is frequently and generally a property of a property. However, in actual usage, the term attribute can and is often treated as equivalent to a property depending on the technology being discussed. An attribute of an object usually consists of a name and a value. For an element these can be a type and class name, while for a file these can be a name and an extension, respectively.
, classes can contain attributes and methods.]] can be represented as a column or field.]] In object-oriented programming, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such. For clarity, attributes should more correctly be considered metadata. An attribute is frequently and generally a property of a property. However, in actual usage, the term attribute can and is often treated as equivalent to a property depending on the technology being discussed. An attribute of an object usually consists of a name and a value. For an element these can be a type and class name, while for a file these can be a name and an extension, respectively. ==Attribute usage== If the element in question could be considered a property ( CUSTOMER_NAME ) of another entity (let's say CUSTOMER ), the element can have zero or more attributes (properties) of its own ( CUSTOMER_NAME is of TYPE = "KINDOFTEXT" ). nodiscard int add(int a, int b, maybe_unused int c) { return a + b; } class Integer { private: no_unique_address int x; public: nodiscard bool isPositive() const noexcept { if (x > 0) likely { return true; } return false; } }; C++26 added annotations, which can be created, and can be accessed using reflection, allowing arbitrary metadata to be attached. import std; using std::string; // some annotations defined in namespace wikipedia::examples using wikipedia::examples::Debug; using wikipedia::examples::EnumFlag; using wikipedia::examples::Rename; enum class =EnumFlag Toggle : uint8_t { OFF, ON, }; struct =Debug Person { =Rename("full name") string fullName; int age; }; // causes compiler message saying that [Obsolete("Use class NewClass instead", IsError = true)] public class ObsoleteClass { // ... } public class NewClass { // ... } // class name ends with "Attribute" // but can be used as "Obsolete" public class ObsoleteAttribute : Attribute { public string Message { get; } public bool IsError { get; set; } public ObsoleteAttribute() {...} public ObsoleteAttribute(string msg) {...} public ObsoleteAttribute(string msg, bool error) {...} } [Obsolete] [Obsolete("This is obsolete")] [Obsolete("This is obsolete", false)] [Obsolete("This is obsolete", IsError = false)] Positional parameters like first parameter of type string above are parameters of the attribute's constructor. Name parameters like the Boolean parameter in the example are a property of the attribute and should be a constant value. Attributes should be contrasted against XML documentation that also defines metadata, but is not included in the compiled assembly and therefore cannot be accessed programmatically. Check me document.getElementById('check1').addEventListener('change', function(e) { var input = this; var p = document.querySelector('p'); p.innerHTML = "input.checked: " + (input.checked ? 'true' : 'false') + " " }); abstract class Animal { public abstract void speak(); public String getType() { return "Generic animal"; } } class Cat extends Animal { @Override public void speak() { System.out.println("Meow!"); } @Override public String getType() { return "Cat"; } } from functools import wraps from typing import Any, Callable, TypeVar T = TypeVar("T") def log_call(func: Callable[..., T]) -> Callable[..., T]: @wraps(func) def wrapper(*args: tuple[int, ...], **kwargs: dict[str, str]) -> T: print(f"Calling {func.__name__} with args={args}, kwargs={kwargs}") return: T = func(*args, **kwargs) print(f"{func.__name__} returned {result}") return result return wrapper @log_call def add(a: int, b: int) -> int: return a + b @log_call def greet(name: str) -> str: return f"Hello, {name}!" In Rust, the syntax of an attribute is #[attr] , placed on any symbol, or even statements. Attributes modify how code is compiled or behaves by attaching metadata to a symbol. In Rust, attributes are a type of procedural macro, which acts on Rust syntax by consuming and producing code. Many macros are defined in the language, but new ones are created using procedural macros. The following are some examples of usage of existing macros: // Marks a function as inline [inline] fn add(a: i32, b: i32) -> i32 { a + b } // Allows unused variables [allow(unused_variables)] fn example() { let x = 5; } // Conditionally compiles the function if the platform is Windows [cfg(target_os = "windows")] fn windows_only() { println!("Running on Windows."); } Rust also supports the #[derive(Ts...)] , which are called "derive macros", used to automatically implement traits. // Point automatically implements traits Debug, Clone, PartialEq [derive(Debug, Clone, PartialEq)] struct Point { x: i32, y: i32, } @available(iOS 17, *) func newFeature() { print("Only available on iOS 17+") } A good example is the process of XML assigning values to properties (elements). Note that the element's value is found before the (separate) end tag, not in the element itself. The element itself may have a number of attributes set ( NAME = "IAMAPROPERTY" ). If the element in question could be considered a property ( CUSTOMER_NAME ) of another entity (let's say CUSTOMER ), the element can have zero or more attributes (properties) of its own ( CUSTOMER_NAME is of TYPE = "KINDOFTEXT" ). ==See also== Access level Attribute (knowledge representation) Variable (computer science) Property (object-oriented programming) Attribute–value pair Field (computer science) File attribute Extended file attributes Java annotation Metadata ==References== sl:Atribut alt sv:Objektorienterad programmering#Attribut
Text derived from https://en.wikipedia.org/w/index.php?oldid=1374228509 under CC BY-SA 4.0; modifications have been made.