鱼C论坛

 找回密码
 立即注册
查看: 1986|回复: 2

求问,为什么第三方库内定义的函数都直接pass

[复制链接]
发表于 2020-9-4 11:01:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求问,为什么第三方库内定义的函数都直接pass,那是如何实现它的功能的呢?
这个是cantera模块的一个ReactorNet类,其中类方法只有一个注释和pass,我想改变其中的step,但完全看不懂它是如何实现的
  1. # encoding: utf-8
  2. # module cantera._cantera
  3. # from C:\Users\Administrator\PycharmProjects\untitled\venv\lib\site-packages\cantera\_cantera.cp37-win_amd64.pyd
  4. # by generator 1.145
  5. # no doc

  6. # imports
  7. import builtins as __builtins__ # <module 'builtins' (built-in)>
  8. import numpy as np # C:\Users\Administrator\PycharmProjects\untitled\venv\lib\site-packages\numpy\__init__.py
  9. import math as math # <module 'math' (built-in)>
  10. import sys as sys # <module 'sys' (built-in)>
  11. import os as os # C:\Program Files\Python37\lib\os.py
  12. import warnings as warnings # C:\Program Files\Python37\lib\warnings.py
  13. import weakref as weakref # C:\Program Files\Python37\lib\weakref.py
  14. import numbers as _numbers # C:\Program Files\Python37\lib\numbers.py
  15. import cantera.interrupts as interrupts # C:\Users\Administrator\PycharmProjects\untitled\venv\lib\site-packages\cantera\interrupts.py

  16. from .object import object

  17. class ReactorNet(object):
  18.     """
  19.     ReactorNet(reactors=())
  20.    
  21.         Networks of reactors. ReactorNet objects are used to simultaneously
  22.         advance the state of one or more coupled reactors.
  23.    
  24.         Example:
  25.    
  26.         >>> r1 = Reactor(gas1)
  27.         >>> r2 = Reactor(gas2)
  28.         >>> <... install walls, inlets, outlets, etc...>
  29.    
  30.         >>> reactor_network = ReactorNet([r1, r2])
  31.         >>> reactor_network.advance(time)
  32.     """
  33.     def add_reactor(self, Reactor_r): # real signature unknown; restored from __doc__
  34.         """
  35.         ReactorNet.add_reactor(self, Reactor r)
  36.         Add a reactor to the network.
  37.         """
  38.         pass

  39.     def advance(self, double_t): # real signature unknown; restored from __doc__
  40.         """
  41.         ReactorNet.advance(self, double t)
  42.         
  43.                 Advance the state of the reactor network in time from the current
  44.                 time to time *t* [s], taking as many integrator timesteps as necessary.
  45.         """
  46.         pass

  47.     def advance_to_steady_state(self, int_max_steps=10000, double_residual_threshold=0., double_atol=0., bool_return_residuals=False): # real signature unknown; restored from __doc__
  48.         """
  49.         ReactorNet.advance_to_steady_state(self, int max_steps=10000, double residual_threshold=0., double atol=0., bool return_residuals=False)
  50.         
  51.                 Advance the reactor network in time until steady state is reached.
  52.         
  53.                 The steady state is defined by requiring that the state of the system
  54.                 only changes below a certain threshold. The residual is computed using
  55.                 feature scaling:
  56.         
  57.                 .. math:: r = \left| \frac{x(t + \Delta t) - x(t)}{\text{max}(x) + \text{atol}} \right| \cdot \frac{1}{\sqrt{n_x}}
  58.         
  59.                 :param max_steps:
  60.                     Maximum number of steps to be taken
  61.                 :param residual_threshold:
  62.                     Threshold below which the feature-scaled residual r should drop such
  63.                     that the network is defines as steady state. By default,
  64.                     residual_threshold is 10 times the solver rtol.
  65.                 :param atol:
  66.                     The smallest expected value of interest. Used for feature scaling.
  67.                     By default, this atol is identical to the solver atol.
  68.                 :param return_residuals:
  69.                     If set to `True`, this function returns the residual time series
  70.                     as a vector with length `max_steps`.
  71.         """
  72.         pass

  73.     def component_name(self, int_i): # real signature unknown; restored from __doc__
  74.         """
  75.         ReactorNet.component_name(self, int i)
  76.         
  77.                 Return the name of the i-th component of the global state vector. The
  78.                 name returned includes both the name of the reactor and the specific
  79.                 component, e.g. `'reactor1: CH4'`.
  80.         """
  81.         pass

  82.     def get_state(self): # real signature unknown; restored from __doc__
  83.         """
  84.         ReactorNet.get_state(self)
  85.         
  86.                 Get the combined state vector of the reactor network.
  87.         
  88.                 The combined state vector consists of the concatenated state vectors of
  89.                 all entities contained.
  90.         """
  91.         pass

  92.     def reinitialize(self): # real signature unknown; restored from __doc__
  93.         """
  94.         ReactorNet.reinitialize(self)
  95.         
  96.                 Reinitialize the integrator after making changing to the state of the
  97.                 system. Changes to Reactor contents will automatically trigger
  98.                 reinitialization.
  99.         """
  100.         pass

  101.     def sensitivities(self): # real signature unknown; restored from __doc__
  102.         """
  103.         ReactorNet.sensitivities(self)
  104.         
  105.                 Returns the sensitivities of all of the solution variables with respect
  106.                 to all of the registered parameters. The normalized sensitivity
  107.                 coefficient :math:`S_{ki}` of the solution variable :math:`y_k` with
  108.                 respect to sensitivity parameter :math:`p_i` is defined as:
  109.         
  110.                 .. math:: S_{ki} = \frac{p_i}{y_k} \frac{\partial y_k}{\partial p_i}
  111.         
  112.                 For reaction sensitivities, the parameter is a multiplier on the forward
  113.                 rate constant (and implicitly on the reverse rate constant for
  114.                 reversible reactions).
  115.         
  116.                 The sensitivities are returned in an array with dimensions *(n_vars,
  117.                 n_sensitivity_params)*, unless no timesteps have been taken, in which
  118.                 case the shape is *(0, n_sensitivity_params)*. The order of the
  119.                 variables (i.e., rows) is:
  120.         
  121.                 `Reactor` or `IdealGasReactor`:
  122.         
  123.                 - 0  - mass
  124.                 - 1  - volume
  125.                 - 2  - internal energy or temperature
  126.                 - 3+ - mass fractions of the species
  127.         
  128.                 `ConstPressureReactor` or `IdealGasConstPressureReactor`:
  129.         
  130.                 - 0  - mass
  131.                 - 1  - enthalpy or temperature
  132.                 - 2+ - mass fractions of the species
  133.         """
  134.         pass

  135.     def sensitivity(self, component, int_p, int_r=0): # real signature unknown; restored from __doc__
  136.         """
  137.         ReactorNet.sensitivity(self, component, int p, int r=0)
  138.         
  139.                 Returns the sensitivity of the solution variable *component* in
  140.                 reactor *r* with respect to the parameter *p*. *component* can be a
  141.                 string or an integer. See `component_index` and `sensitivities` to
  142.                 determine the integer index for the variables and the definition of the
  143.                 resulting sensitivity coefficient. If it is not given, *r* defaults to
  144.                 the first reactor. Returns an empty array until the first time step is
  145.                 taken.
  146.         """
  147.         pass

  148.     def sensitivity_parameter_name(self, int_p): # real signature unknown; restored from __doc__
  149.         """
  150.         ReactorNet.sensitivity_parameter_name(self, int p)
  151.         
  152.                 Name of the sensitivity parameter with index *p*.
  153.         """
  154.         pass

  155.     def set_initial_time(self, double_t): # real signature unknown; restored from __doc__
  156.         """
  157.         ReactorNet.set_initial_time(self, double t)
  158.         
  159.                 Set the initial time. Restarts integration from this time using the
  160.                 current state as the initial condition. Default: 0.0 s.
  161.         """
  162.         pass

  163.     def set_max_time_step(self, double_t): # real signature unknown; restored from __doc__
  164.         """
  165.         ReactorNet.set_max_time_step(self, double t)
  166.         
  167.                 Set the maximum time step *t* [s] that the integrator is allowed
  168.                 to use.
  169.         """
  170.         pass

  171.     def step(self): # real signature unknown; restored from __doc__
  172.         """
  173.         ReactorNet.step(self)
  174.         
  175.                 Take a single internal time step. The time after taking the step is
  176.                 returned.
  177.         """
  178.         pass

  179.     def __copy__(self): # real signature unknown; restored from __doc__
  180.         """ ReactorNet.__copy__(self) """
  181.         pass

  182.     def __init__(self, reactors=()): # real signature unknown; restored from __doc__
  183.         pass

  184.     @staticmethod # known case of __new__
  185.     def __new__(*args, **kwargs): # real signature unknown
  186.         """ Create and return a new object.  See help(type) for accurate signature. """
  187.         pass

  188.     def __reduce__(self): # real signature unknown; restored from __doc__
  189.         """ ReactorNet.__reduce__(self) """
  190.         pass

  191.     atol = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  192.     """
  193.         The absolute error tolerance used while integrating the reactor
  194.         equations.
  195.         """

  196.     atol_sensitivity = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  197.     """
  198.         The absolute error tolerance for sensitivity analysis.
  199.         """

  200.     max_err_test_fails = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  201.     """
  202.         The maximum number of error test failures permitted by the CVODES
  203.         integrator in a single time step.
  204.         """

  205.     n_sensitivity_params = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  206.     """
  207.         The number of registered sensitivity parameters.
  208.         """

  209.     n_vars = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  210.     """
  211.         The number of state variables in the system. This is the sum of the
  212.         number of variables for each `Reactor` and `Wall` in the system.
  213.         Equal to:

  214.         `Reactor` and `IdealGasReactor`: `n_species` + 3 (mass, volume,
  215.         internal energy or temperature).

  216.         `ConstPressureReactor` and `IdealGasConstPressureReactor`:
  217.         `n_species` + 2 (mass, enthalpy or temperature).

  218.         `Wall`: number of surface species
  219.         """

  220.     rtol = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  221.     """
  222.         The relative error tolerance used while integrating the reactor
  223.         equations.
  224.         """

  225.     rtol_sensitivity = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  226.     """
  227.         The relative error tolerance for sensitivity analysis.
  228.         """

  229.     time = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  230.     """The current time [s]."""

  231.     verbose = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
  232.     """
  233.         If *True*, verbose debug information will be printed during
  234.         integration. The default is *False*.
  235.         """



复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-4 11:53:10 | 显示全部楼层
因为不是用python写的,懂吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-4 12:21:51 From FishC Mobile | 显示全部楼层
本帖最后由 hrp 于 2020-9-4 12:41 编辑

专门定义只有pass的Python空函数,是为了让用户能够在Python程序中实现对这些函数的重写以实现某执行些流程的变化。

比如一个模块中某个类有退出方法exit,这个程序是用C写的,python用户无法去重写这个方法。这个exit方法内部的实现是这样的:先调用执行python写的空函数close_event,再执行exit剩下的语句后退出。当用户程序继承这个类时,如果不重写close_event,那么执行exit后只会正常退出。如果重写了close_event比如保存某些文件,那执行exit时,程序就会先保存某些某些文件再退出,这样就实现了退出流程的自定义。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 17:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表