python中class的定义及使用 python当中class用法

您好,今天小源来为大家解答以上的问题。python中class的定义及使用相信很多小伙伴还不知道,现在让我们一起来看看吧!

python中class的定义及使用 python当中class用法python中class的定义及使用 python当中class用法


python中class的定义及使用 python当中class用法


1、它是python语法中固定的方法,主要作用就是创建类实例后做初始化工作,创建时调用__new__,初始化时调用__init__。

2、这些方法叫做魔法方法,由python解释器调用#----------------------------------------------------------------------# Copyright (c) 1999-2001, Digital Creations, Frederickurg, VA, USA# and Andrew Kuchling. All rights reserved.## Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions are# met:## o Redistributions of source code must retain the above copyright# not, this list of conditions, and the disclaimer that follows.## o Redistributions in binary form must reproduce the above copyright# not, this list of conditions, and the following disclaimer in# the documentation and/or other materials provided with the# distribution.## o Neither the name of Digital Creations nor the names of its# contributors may be used to endorse or promote products derived# from this software without specific prior written permission.## THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS AS# IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO SHALL DIGITAL# CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH# DAMAGE.#---------------------------------------------------------------------- """Support for Berkeley DB 4.3 through 5.3 with a intece.For the full featured object oriented intece use the bsddb.db moduleinstead. It rors the Oracle Berkeley DB C API."""import sysabsolute_import = (sys.version_[0] >= 3)if (sys.version_ >= (2, 6)) and (sys.version_ import warningsif sys.py3kwarning and (__name__ != 'bsddb3') :warnings.warnpy3k("in 3.x, the bsddb module has been removed; ""please use the pybsddb project instead",DeprecationWarning, 2)warnings.filterwarnings("ignore", ".CObject.", DeprecationWarning,"bsddb.__init__")try:if __name__ == 'bsddb3':# import _pybsddb binary as it should be the more recent version from# a standalone pybsddb addon package than the version included with# python as bsddb._bsddb.if absolute_import :# Because this syntaxis is not valid before Python 2.5exec("from . import _pybsddb")else :import _pybsddb_bsddb = _pybsddbfrom bsddb3.dbutils import DeadlockWrap as _DeadlockWrapelse:import _bsddbfrom bsddb.dbutils import DeadlockWrap as _DeadlockWrcept ImportError:# Remove ourselves from sys.modulesimport sysdel sys.modules[__name__]raise# bsddb3 calls it db, but provide _db for backwards compatibilitydb = _db = _bsddb__version__ = db.__version__error = db.DBError # So bsddb.error will mean soming...#----------------------------------------------------------------------import sys, osfrom weakref import refif sys.version_ import UserDictMutableMapping = UserDict.DictMixinelse :import collectionsMutableMapping = collections.MutableMappingclass _iter_mixin(MutableMapping):def _make_iter_cursor(self):cur = _DeadlockWrap(self.db.cursor)key = id(cur)self._cursor_refs[key] = ref(cur, self._gen_cref_cleaner(key))return curdef _gen_cref_cleaner(self, key):# use generate the function for the weakref callback here# to ensure that we do not hold a strict reference to cur# in the callback.return lambda ref: self._cursor_refs.pop(key, None)def __iter__(self):self._kill_iteration = Falseself._in_iter += 1try:try:cur = self._make_iter_cursor()# FIXME-20031102-greg: race condition. cursor could# be closed by another thread before this call.# since we're only returning keys, we call the cursor# mods with flags=0, dlen=0, dofs=0key = _DeadlockWrap(cur.first, 0,0,0)[0]yield keynext = getattr(cur, "next")while 1:try:key = _DeadlockWrap(next, 0,0,0)[0]yield keyexcept _bsddb.DBCursorClosedError:if self._kill_iteration:raise RuntimeError('Database changed size ''during iteration.')cur = self._make_iter_cursor()# FIXME-20031101-greg: race condition. cursor could# be closed by another thread before this call._DeadlockWrap(cur.set, key,0,0,0)next = getattr(cur, "next")except _bsddb.DBNotFoundError:pascept _bsddb.DBCursorClosedError:# the database was modified during iteration. abort.pass# When Python 2.4 not supported in bsddb3, we can change this to "finally"except :__init__就是相当于别的oop语言里的构造函数可以在创建对象的时候传入参数,参数该如何使用就是init函数做的。

3、类似c++ 的构造函数。

本文到这结束,希望上面文章对大家有所帮助。

版权声明:图片、内容均来源于互联网 如有侵权联系836084111@qq.com 删除