How to resolve the algorithm Compound data type step by step in the Python programming language
How to resolve the algorithm Compound data type step by step in the Python programming language
Table of Contents
Problem Statement
Create a compound data type:
A compound data type is one that holds multiple independent values.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Compound data type step by step in the Python programming language
This code defines different ways to create objects and access their attributes.
It first defines X
and Y
as constants 0 and 1 respectively.
Then it defines a tuple p
and assigns it the values 3 and 4.
Next, it defines a new list
p
and assigns it the values 3 and 4.
Then, it prints the first element of the list p
, which is 3
.
Next, it defines a class Point with two attributes, x
and y
.
It defines a constructor that takes two parameters, x
and y
, and assigns them to the attributes of the object.
It creates a new instance of the class Point
and prints the value of the attribute x
, which is 0
.
Next, it defines a class MyObject that inherits from the class object. It creates a new instance of the class MyObject
and assigns the values 0 and 1 to the attributes x
and y
.
It then tries to assign the values 0 and 1 to the attributes x
and y
of the class object, but it fails because the class object does not have these attributes.
Finally, it defines a dictionary pseudo_object with two keys, x
and y
, and assigns them the values 1 and 2 respectively.
Finally, it uses the namedtuple function from the collections module to create a new class Point with two attributes, x
and y
.
It creates a new instance of the class Point
and assigns the values 11 and 22 to the attributes x
and y
.
It then prints the value of the attribute x
, which is 11.
Source code in the python programming language
X, Y = 0, 1
p = (3, 4)
p = [3, 4]
print p[X]
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
p = Point()
print p.x
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
pseudo_object = {'x': 1, 'y': 2}
>>> 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)
>>>
You may also check:How to resolve the algorithm Continued fraction step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Power set step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Picat programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the V (Vlang) programming language