Skip to content

Commit 9a3b94e

Browse files
committed
updates
1 parent e5595d0 commit 9a3b94e

35 files changed

+738
-382
lines changed

source/docs/bool/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Constructors
88
------------
99
`bool()`_
1010
Returns an expression converted into a Boolean.
11-
`Literal Syntax`_
12-
11+
`literal syntax`_
12+
Initializes a new instance of the **bool** type.
1313

1414
.. _literal syntax: literals.html
1515
.. _bool(): ../functions/bool.html

source/docs/complex/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Constructors
1010
====
1111
`complex()`_
1212
Returns an expression converted into a complex number.
13-
`Literal Syntax`_
13+
`literal syntax`_
14+
Initializes a new instance of the **complex** type.
1415

1516
Properties
1617
====

source/docs/dict/index.rst

+29-28
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,7 @@ Constructors
1313
`{} dict comprehension`_
1414
Returns a dictionary based on existing iterables.
1515
`literal syntax`_
16-
17-
Misc
18-
====
19-
`[] (key lookup)`_
20-
Returns the value associated with the given key.
21-
22-
Functions
23-
====
24-
`len`_
25-
Returns an int type specifying number of elements in the collection.
26-
`min`_
27-
Returns the smallest item from a collection.
28-
`max`_
29-
Returns the largest item in an iterable or the largest of two or more arguments.
30-
`sum`_
31-
Returns a total of the items contained in the iterable object.
32-
`sorted`_
33-
Returns a sorted list from the iterable.
34-
`reversed`_
35-
Returns a reverse iterator over a sequence.
36-
`all`_
37-
Returns a Boolean value that indicates whether the collection contains only values that evaluate to True.
38-
`any`_
39-
Returns a Boolean value that indicates whether the collection contains any values that evaluate to True.
40-
`enumerate`_
41-
Returns an enumerate object.
42-
`zip`_
43-
Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
16+
Initializes a new instance of the **dict** type.
4417

4518
Methods
4619
====
@@ -110,6 +83,34 @@ Dictionary Views Operators
11083
`| (union)`_
11184
Returns all the elements that appear in the dictview and the specified iterable.
11285

86+
Functions
87+
====
88+
`len`_
89+
Returns an int type specifying number of elements in the collection.
90+
`min`_
91+
Returns the smallest item from a collection.
92+
`max`_
93+
Returns the largest item in an iterable or the largest of two or more arguments.
94+
`sum`_
95+
Returns a total of the items contained in the iterable object.
96+
`sorted`_
97+
Returns a sorted list from the iterable.
98+
`reversed`_
99+
Returns a reverse iterator over a sequence.
100+
`all`_
101+
Returns a Boolean value that indicates whether the collection contains only values that evaluate to True.
102+
`any`_
103+
Returns a Boolean value that indicates whether the collection contains any values that evaluate to True.
104+
`enumerate`_
105+
Returns an enumerate object.
106+
`zip`_
107+
Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
108+
109+
Misc
110+
====
111+
`[] (key lookup)`_
112+
Returns the value associated with the given key.
113+
113114
.. _dict(): ../functions/dict.html
114115
.. _[] (key lookup): ../brackets/key_lookup.html
115116
.. _{} dict comprehension: ../comprehensions/dict_comprehension.html
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
====
2-
Double Underscore Methods and Variables
2+
Direct Attribute Access
33
====
44

5-
Direct Atribute Access
5+
The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of x.name) for class instances.
6+
7+
Methods
68
====
79
`\__getattribute__`_
810
Called unconditionally to implement attribute accesses for instances of the class.
@@ -13,31 +15,8 @@ Direct Atribute Access
1315
`\__delattr__`_
1416
Called when an attribute deletion is attempted.
1517

16-
Descriptor Protocol
17-
====
18-
Used to create object attributes overriding attribute access for class instances.
19-
20-
Comparisons
21-
================
22-
23-
Containers
24-
==========
25-
26-
Context Managers
27-
================
28-
29-
Numeric Methods
30-
===============
31-
32-
Object Attributes
33-
=================
34-
35-
Pickle Protocol
36-
===============
37-
3818
.. _\__getattribute__: ./getattribute.html
3919
.. _\__getattr__: ./getattr.html
4020
.. _\__setattr__: ./setattr.html
4121
.. _\__delattr__: ./delattr.html
42-
43-
.. _Descriptor ProtocolTODO: ./dscrpt.html
22+
File renamed without changes.

source/docs/dunderdsc/delete.rst

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
====
2+
__delete__
3+
====
4+
5+
Description
6+
====
7+
Called to delete the attribute on an instance instance of the owner class.
8+
9+
Syntax
10+
====
11+
**object**. *__delete__(self, instance)*
12+
13+
*self*
14+
Required. Instance of the class, passed automatically on call.
15+
*instance*
16+
Required. Instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner.
17+
18+
Remarks
19+
====
20+
The following method only apply to new-style classes.
21+
22+
The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents).
23+
24+
This method should delete attribute value if found or raise an AttributeError exception otherwise.
25+
26+
Example
27+
====
28+
::
29+
30+
# this is our descriptor object
31+
class Bar(object):
32+
def __init__(self):
33+
self.value = ''
34+
def __get__(self, instance, owner):
35+
print "returned from descriptor object"
36+
return self.value
37+
def __set__(self, instance, value):
38+
print "set in descriptor object"
39+
self.value = value
40+
def __delete__(self, instance):
41+
print "deleted in descriptor object"
42+
del self.value
43+
44+
class Foo(object):
45+
bar = Bar()
46+
47+
f = Foo()
48+
f.bar = 10
49+
print f.bar
50+
del f.bar
51+
52+
set in descriptor object
53+
returned from descriptor object
54+
10
55+
deleted in descriptor object

source/docs/dunderdsc/get.rst

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
====
2+
__get__
3+
====
4+
5+
Description
6+
====
7+
Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access).
8+
9+
Syntax
10+
====
11+
**object**. *__get__(self, instance, owner)*
12+
13+
*self*
14+
Required. Instance of the class, passed automatically on call.
15+
*instance*
16+
Required. Instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner.
17+
*owner*
18+
Required. Owner is always the owner class.
19+
20+
Remarks
21+
====
22+
The following method only applies to new-style classes.
23+
24+
The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents).
25+
26+
This method should return the (computed) attribute value or raise an AttributeError exception.
27+
28+
Example
29+
====
30+
::
31+
32+
# this is our descriptor object
33+
class Bar(object):
34+
def __init__(self):
35+
self.value = ''
36+
def __get__(self, instance, owner):
37+
print "returned from descriptor object"
38+
return self.value
39+
def __set__(self, instance, value):
40+
print "set in descriptor object"
41+
self.value = value
42+
def __delete__(self, instance):
43+
print "deleted in descriptor object"
44+
del self.value
45+
46+
class Foo(object):
47+
bar = Bar()
48+
49+
f = Foo()
50+
f.bar = 10
51+
print f.bar
52+
del f.bar
53+
54+
set in descriptor object
55+
returned from descriptor object
56+
10
57+
deleted in descriptor object

source/docs/dunderdsc/index.rst

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
====
2+
Descriptor Protocol
3+
====
4+
5+
In general, a descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor protocol: __get__(), __set__(), and __delete__(). If any of those methods are defined for an object, it is said to be a descriptor.
6+
7+
The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, a.x has a lookup chain starting with a.__dict__['x'], then type(a).__dict__['x'], and continuing through the base classes of type(a) excluding metaclasses.
8+
9+
However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. Note that descriptors are only invoked for new style objects or classes (ones that subclass object() or type()).
10+
11+
The starting point for descriptor invocation is a binding, a.x. How the arguments are assembled depends on a:
12+
13+
Direct Call
14+
The simplest and least common call is when user code directly invokes a descriptor method: x.__get__(a).
15+
Instance Binding
16+
If binding to a new-style object instance, a.x is transformed into the call: type(a).__dict__['x'].__get__(a, type(a)).
17+
Class Binding
18+
If binding to a new-style class, A.x is transformed into the call: A.__dict__['x'].__get__(None, A).
19+
Super Binding
20+
If a is an instance of super, then the binding super(B, obj).m() searches obj.__class__.__mro__ for the base class A immediately preceding B and then invokes the descriptor with the call: A.__dict__['m'].__get__(obj, obj.__class__).
21+
22+
For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of __get__(), __set__() and __delete__(). If it does not define __get__(), then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines __set__() and/or __delete__(), it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both __get__() and __set__(), while non-data descriptors have just the __get__() method. Data descriptors with __set__() and __get__() defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances.
23+
24+
Python methods (including staticmethod() and classmethod()) are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class.
25+
26+
The property() function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property.
27+
28+
Methods
29+
====
30+
`\__get__`_
31+
Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access).
32+
`\__set__`_
33+
Called to set the attribute on an instance instance of the owner class to a new value, value.
34+
`\__delete__`_
35+
Called to delete the attribute on an instance instance of the owner class.
36+
37+
38+
.. _\__get__: get.html
39+
.. _\__set__: set.html
40+
.. _\__delete__: delete.html

source/docs/dunderdsc/set.rst

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
====
2+
__set__
3+
====
4+
5+
Description
6+
====
7+
Called to set the attribute on an instance instance of the owner class to a new value, value.
8+
9+
Syntax
10+
====
11+
**object**. *__set__(self, instance, value)*
12+
13+
*self*
14+
Required. Instance of the class, passed automatically on call.
15+
*instance*
16+
Required. Instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner.
17+
*value*
18+
Required. The value we want to assign to the attribute.
19+
20+
Remarks
21+
====
22+
The following method only apply to new-style classes.
23+
24+
The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents).
25+
26+
Example
27+
====
28+
::
29+
30+
# this is our descriptor object
31+
class Bar(object):
32+
def __init__(self):
33+
self.value = ''
34+
def __get__(self, instance, owner):
35+
print "returned from descriptor object"
36+
return self.value
37+
def __set__(self, instance, value):
38+
print "set in descriptor object"
39+
self.value = value
40+
def __delete__(self, instance):
41+
print "deleted in descriptor object"
42+
del self.value
43+
44+
class Foo(object):
45+
bar = Bar()
46+
47+
f = Foo()
48+
f.bar = 10
49+
print f.bar
50+
del f.bar
51+
52+
set in descriptor object
53+
returned from descriptor object
54+
10
55+
deleted in descriptor object

0 commit comments

Comments
 (0)