Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
5
Task/Compound-data-type/Python/compound-data-type-1.py
Normal file
5
Task/Compound-data-type/Python/compound-data-type-1.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
X, Y = 0, 1
|
||||
p = (3, 4)
|
||||
p = [3, 4]
|
||||
|
||||
print p[X]
|
||||
7
Task/Compound-data-type/Python/compound-data-type-2.py
Normal file
7
Task/Compound-data-type/Python/compound-data-type-2.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class Point:
|
||||
def __init__(self, x=0, y=0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
p = Point()
|
||||
print p.x
|
||||
5
Task/Compound-data-type/Python/compound-data-type-3.py
Normal file
5
Task/Compound-data-type/Python/compound-data-type-3.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class MyObject(object): pass
|
||||
point = MyObject()
|
||||
point.x, point.y = 0, 1
|
||||
# objects directly instantiated from "object()" cannot be "monkey patched"
|
||||
# however this can generally be done to it's subclasses
|
||||
1
Task/Compound-data-type/Python/compound-data-type-4.py
Normal file
1
Task/Compound-data-type/Python/compound-data-type-4.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
pseudo_object = {'x': 1, 'y': 2}
|
||||
27
Task/Compound-data-type/Python/compound-data-type-5.py
Normal file
27
Task/Compound-data-type/Python/compound-data-type-5.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
>>> from collections import namedtuple
|
||||
>>> help(namedtuple)
|
||||
Help on function namedtuple in module collections:
|
||||
|
||||
namedtuple(typename, field_names, verbose=False)
|
||||
Returns a new subclass of tuple with named fields.
|
||||
|
||||
>>> Point = namedtuple('Point', 'x y')
|
||||
>>> Point.__doc__ # docstring for the new class
|
||||
'Point(x, y)'
|
||||
>>> p = Point(11, y=22) # instantiate with positional args or keywords
|
||||
>>> p[0] + p[1] # indexable like a plain tuple
|
||||
33
|
||||
>>> x, y = p # unpack like a regular tuple
|
||||
>>> x, y
|
||||
(11, 22)
|
||||
>>> p.x + p.y # fields also accessable by name
|
||||
33
|
||||
>>> d = p._asdict() # convert to a dictionary
|
||||
>>> d['x']
|
||||
11
|
||||
>>> Point(**d) # convert from a dictionary
|
||||
Point(x=11, y=22)
|
||||
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
|
||||
Point(x=100, y=22)
|
||||
|
||||
>>>
|
||||
Loading…
Add table
Add a link
Reference in a new issue