定制数据对象

来源:互联网 发布:优越留学知乎 编辑:程序博客网 时间:2024/06/05 09:27

1、创建字典

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> cheese={}


>>> type(cheese)
<class 'dict'>

>>> cheese['Name']='john'
>>> cheese['Position']='manager'

>>> cheese
{'Position': 'manager', 'Name': 'john'}

>>> cheese['Age']='25'
>>> cheese
{'Position': 'manager', 'Age': '25', 'Name': 'john'}
>>> 


2、将代码与数据打包在类中


创建类

 


class Athlete


def  _init_(self):

#the code to initialize a "Athlete" object


创建对象

 a=Athlete();--------------Athlete._init_(a)

每个方法的第一个参数为调用对象实例

class Athlete


def  _init_(self,value=0):

self.thing=value

def  howbig(self):

return(len(self.thing))

  • 无返回值啊 
  • 第一个参数为调用对象





原创粉丝点击