内置方法
from
collections
import
namedtuple
free_falling_body
=
namedtuple
(
'free_falling_body'
,
[
'g'
,
't'
]
)
h
=
free_falling_body
(
9.8
,
2
**
(
1
/
2
)
)
print
(
h
)
# 自由落体运动
print
(
h
.
g
*
h
.
t
**
2
/
2
)
# 自由落体高度
free_falling_body(g=9.8, t=1.4142135623730951)
9.800000000000002
自创
class
Tuple
:
t
=
(
'a'
,
'b'
,
'c'
)
for
i
in
t
:
exec
(
"%s='%s'"
%
(
i
,
i
)
)
print
(
' '
.
join
(
Tuple
.
t
)
)
print
(
Tuple
.
b
,
Tuple
.
i
)
print
(
Tuple
.
__dict__
)
打印结果
a b c
b c
{
'__module__'
:
'__main__'
,
't'
:
(
'a'
,
'b'
,
'c'
)
,
'i'
:
'c'
,
'a'
:
'a'
,
'b'
:
'b'
,
'c'
:
'c'
,
'__dict__'
:
<
attribute
'__dict__'
of
'Tuple'
objects
>
,
'__weakref__'
:
<
attribute
'__weakref__'
of
'Tuple'
objects
>
,
'__doc__'
:
None
}