《head first python》——定制数据对象

一、实用字典关联数据

二、定义一个类

每个类都有一个__init__()方法来控制初始化对象,除了__init__(),每个方法第一个参数都是self

>>> class Athelete:
	def __init__(self):
		self.thing = value
	def how_big(self):
		return(len(self.thing))
需要注意:self是默认的,类的属性可以在类的参数里定义好

>>> class Athelete:
	def __init__(self,a_name,a_dob=None,a_times=[]):
		self.name = a_name
		self.dob = a_dob
		self.times = a_times
	def add_time(self,time_value):
		self.times.append(time_value)
	def add_times(self,list_of_times):
		self.times.extend(list_of_times)

		
>>> sarah=Athelete("sarah",'2002-06-07',['a:58','1.56'])
>>> sarah.add_time('3.25')
>>> sarah
<__main__.Athelete object at 0x000000000553D6A0>
>>> sarah.times
['a:58', '1.56', '3.25']
>>> 

与列表区别:Athelete很像列表,add_time()和add_times()其实是包装过的append()和extend()函数。区别只是Athelete类包含name和dob对象。

虽然python支持从零建立一个定制类,但也可以继承一个已有类,包括:list、set,并使用他们内置的数据结构

>>> class NameList(list):
	def __init__(self,a_name):
		self.name = a_name

		
>>> john = NameList("john name")
>>> type(john)
<class '__main__.NameList'>
>>> dir(john)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'name', 'pop', 'remove', 'reverse', 'sort']
>>> 
john能做列表能做的所有事,并且可添加属性:

>>> john.append("player")
>>> john.extend(["student","boy"])
>>> for attr in john:
	print(john.name+" is a "+ attr + ".")

john name is a player.
john name is a student.
john name is a boy.
>>> 

实战:

<pre name="code" class="python">>>> testlist = [['1', '2009-03-02', 3.762146, -8],
['1', '2009-03-01', 3.758066, -8],
['1', '2009-01-03', 3.76085332653061, -8],
['0', '2009-12-28', 39.759765375, -104.984303125],
['0', '2009-12-27', 39.70999875, -104.95675168749999],
['0', '2009-12-24', 39.72202824, -104.96074968],
['0', '2009-12-23', 39.77230825, -105.0435074375],
['0', '2009-12-21', 39.77455175, -105.01426466666665],
['0', '2009-12-20', 39.76241472, -104.90343176000002],
['299', '2009-08-14', 29.894691, -81.314517],
['299', '2009-08-13', 29.894691000000005, -81.314517],
]
>>> class nodeList(list):
	def __init__(self,m_1=0,m_2=0,probability=1):
		list.__init__([])
		self.m_1 = m_1
		self.m_2 = m_2
		self.probability = probability
	def probability(m_1,m_2):
		info = a*sum([1/2**i for i in range(m_1)]) + \
				b*sum([1/2**i for i in range(m_2)])
		probability = 1 - info
		self.probability = probability
>>> type(testlist[0])
<class 'list'>
>>> temp = nodeList()
>>> temp.extend(testlist[0])
>>> type(temp)
<class '__main__.nodeList'>
>>> testlist[0]=temp
>>> type(testlist[0])
<class '__main__.nodeList'>
>>> testlist[0].m_1
0

>>> for item in testlist:
	temp = nodeList()
	temp.extend(item)
	item = temp

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读