本文共 3341 字,大约阅读时间需要 11 分钟。
class Human: # self有且必须有, 而且要放第一个参数的位置, 而且所有的方法也需要self放在第一位置. def __init__(self, v1, v2, v3): self.name = v1 self.sex = v2 self.age = v3 def how_old(self): return(self.age)实例化
>>> digoal=Human('digoal','male',32)>>> digoal.name'digoal'>>> digoal.sex'male'>>> digoal.age32>>> digoal.how_old()32>>> digoal.age=100>>> digoal.how_old()100使用dir()函数返回对象内的方法, 变量等.
>>> dir(digoal)['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'how_old', 'name', 'sex']不带参数输出的是当前的变量
>>> a=1>>> dir()['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a']输出变量类型
>>> type(digoal)输出变量位置
>>> digoal<__main__.Human object at 0x7fb4bf5aabe0>
>>> class NamedList(list):... def __init__(self, v_name='',v_l=[]):... list.__init__([]) # 扩展类list初始化... self.name=v_name... self.extend(v_l)... >>> a=NamedList('abc')>>> a.name'abc'NamedList包含了list的所有变量和方法.
>>> dir(a)['__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']>>> a.extend([1,2,3,4])>>> a[1, 2, 3, 4]>>> a.reverse()>>> a[4, 3, 2, 1][其他]
1. Create a empty dictionary using the dict() factory function or using {} .创建空字典(类似json), 使用dict()构造函数, 或{}构造器例子 : To access the value associated with the key Name in a dictionary called person, use the familiar square bracket notation: person['Name'] .Like list and set, a Python's dictionary dynamically grows as new data is added to the data structure.Populate a dictionary as you go: new_d = {} or new_d = dict() and then d['Name'] = 'Eric Idle' or do the same thing all in the one go: new_d = {'Name': 'Eric Idle'}2. The class keyword lets you define a class.定义类, 使用class关键字, 一般类名称大写开头, 继承类需要在类名称后加上继承类名作为参数例如class NamedList(list):3. Class methods (your code) are defined in much the same way as functions, that is, with the def keyword. Class attributes (your data) are just like variables that exist within object instances. The __init__() method can be defined within a class to initialize object instances. Every method defined in a class must provide self as its first argument.类方法定义和函数定义类似, 使用def method_name(self, 其他参数):类方法必须使用self参数作为第一个参数.__init__()用于创建实例时初始化实例.4. Every attribute in a class must be prefixed with self. in order to associate it data with its instance.类的定义中, 所有类的属性都必须以self开头. 例如self.name5. Classes can be built from scratch or can inherit from Python's built-in classes or from other custom classes.6. Classes can be put into a Python module and uploaded to PyPI.类可以放到模块中, 并upload到PyPI.
转载地址:http://qkogx.baihongyu.com/