Attributes¶
util.attribute gives objects named, typed values – attributes –
that are inherited down a type hierarchy, the way a class’s members are
inherited by its subclasses and instances, but at run time and under rules you
set per attribute.
Attributes and attributed objects¶
An Attribute is a value with:
a type: a Python type, or the name of one of the known types –
int,float,bool,str,textandmtext(multi-line text),color,list,dict,attrs, orchoices[...](one of a list of values). The names tell an editor how to edit the value (see Tk Widgets);a restriction on how it’s inherited (below);
a note: a description, shown as a tooltip by the attribute editor.
An AttributedObject is a dict of attributes, by
name, that also inherits attributes from its supertypes. A subclass says what
those are, and whether the object is a type or an individual (an instance), by
overriding getSupers() and
isIndividual(). Then:
obj.get(key)is the attribute’s effective value: the object’s own, or else the one it inherits (searching its supertypes in order);obj.getLocal(key)andobj.getInh(key)give just one or the other;obj[key] = valuesets the object’s own value.
get() can also store a default – obj.get(key, storeDefault=v,
storeType=t, note=...) – which is how an application declares its
attributes (and preferences: see Preferences, Graphs and Utilities) as it first uses them.
An AttributedObject is a Subject: it announces
"new attribute", "mod attribute" and "del attribute", so that,
eg: the drawing of an object can follow its attributes. It’s
Serializable and
Dirtyable too.
Inheritance restrictions¶
A Restriction holds two
RestrictionTypes flags: one for how individuals
inherit the attribute, and one for how subtypes do:
NOINHERITThe key is inherited, but not the value: it’s a slot that must be filled locally.
NOOVERRIDEThe inherited value can’t be overridden.
PRIVATEThe attribute isn’t inherited at all.
The useful combinations have names:
restriction |
individuals |
subtypes |
eg |
|---|---|---|---|
|
inherit |
inherit |
a default that anything may change |
|
must fill in |
inherit |
an object’s name |
|
can’t change |
inherit |
a constant that subtypes may change |
|
can’t change |
can’t change |
a constant |
|
must fill in |
must fill in |
a slot everything below must fill |
|
can’t change |
must fill in |
the name of a type |
|
don’t see it |
inherit |
something only for types |
|
don’t see it |
don’t see it |
only for the object itself |
(Some combinations make no sense, and are refused: see
legalityError().)
The attribute editor¶
util.attributeEditor edits an AttributedObject’s attributes in a
dialog: their values, types and restrictions, with the values they inherit
shown above them – see Tk Widgets.