GVKun编程网logo

函数名称在python类中未定义(函数名称在python类中未定义怎么办)

8

想了解函数名称在python类中未定义的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于函数名称在python类中未定义怎么办的相关问题,此外,我们还将为您介绍关于NameError:Pyth

想了解函数名称在python类中未定义的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于函数名称在python类中未定义怎么办的相关问题,此外,我们还将为您介绍关于NameError:Python 3 中未定义全局名称“xrange”、python – Pylint无效的函数名称、python 里怎么样通过函数名称来获取函数地址、Python,名称未定义的新知识。

本文目录一览:

函数名称在python类中未定义(函数名称在python类中未定义怎么办)

函数名称在python类中未定义(函数名称在python类中未定义怎么办)

我对python相对较新,并且在命名空间方面遇到一些问题。

class a:    def abc(self):        print "haha"     def test(self):        abc()b = a()b.abc() #throws an error of abc is not defined. cannot explain why is this so

答案1

小编典典

由于test()不知道是谁abc,因此NameError: global name ''abc'' is notdefined您看到的味精应该在调用时发生b.test()(调用b.abc()很好),请将其更改为:

class a:    def abc(self):        print "haha"     def test(self):        self.abc()          # abc()b = a()b.abc() #  ''haha'' is printedb.test() # ''haha'' is printed

NameError:Python 3 中未定义全局名称“xrange”

NameError:Python 3 中未定义全局名称“xrange”

运行 python 程序时出现错误:

Traceback (most recent call last):  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in <module>  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in __init__  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\class\inventory.py", line 17, in __init__builtins.NameError: global name ''xrange'' is not defined

游戏从这里开始。

是什么导致了这个错误?

答案1

小编典典

您正在尝试使用 Python 3 运行 Python 2 代码库。在 Python
3xrange()中已重命名为range()

改为使用 Python 2 运行游戏。除非您知道自己在做什么,否则不要尝试移植它,很可能会出现更多xrange()问题range()

作为记录,您看到的不是语法错误,而是运行时异常。


如果您确实知道自己在做什么并且正在积极制作与 Python 3 兼容的 Python 2 代码库,则可以通过将全局名称添加到模块作为range.
(考虑到您 可能 必须更新range()Python 2 代码库中的任何现有使用,list(range(...))以确保您仍然在 Python
3 中获得列表对象):

try:    # Python 2    xrangeexcept NameError:    # Python 3, xrange is now named range    xrange = range# Python 2 code that uses xrange(...) unchanged, and any# range(...) replaced with list(range(...))

或者在代码库中替换所有使用xrange(...)with range(...),然后使用不同的 shim 使 Python 3 语法与 Python
2 兼容:

try:    # Python 2 forward compatibility    range = xrangeexcept NameError:    pass# Python 2 code transformed from range(...) -> list(range(...)) and# xrange(...) -> range(...).

后者对于希望 在长期内与 Python 3 兼容的代码库更可取,然后尽可能使用 Python 3 语法更容易。

python – Pylint无效的函数名称

python – Pylint无效的函数名称

我用 Python 3.6.2运行Pylint 1.7.2. Pylint显示以下错误:
Invalid function name "create_maximization_option_dataframe" (invalid-name)

我在我的代码中定义了一个类似的函数:

def create_maximization_option_dataframe(file_name):

PEP8风格指南基本上只是说:

Function names should be lowercase,with words separated by underscores as necessary to improve readability.

据我所知,我正在遵循函数名称的所有格式规则. Pylint是否有一些我不知道的内置最大函数名称长度规则?我可以很容易地忽略Pylint错误,但我想先了解为什么会这样.

解决方法

通过pylint pylint –generate-rcfile创建配置文件.这个范围取决于你把它放在哪里.引用 https://docs.pylint.org/en/1.6.0/run.html

>当前工作目录中的pylintrc
> .pylintrc in
当前的工作目录
>如果当前工作目录位于
在python模块中,Pylint搜索python模块的层次结构
直到它找到一个pylintrc文件.这允许您指定编码
逐个模块的标准.当然,目录是
如果它包含一个init.py文件,则判断它是一个python模块.
>由环境变量PYlintrC命名的文件
>如果你有家
您的主目录中不是/ root:.pylintrc的目录
主目录中的.config / pylintrc
> / etc / pylintrc

听起来你需要选项5或6.

在pylintrc中,找到这个位

# Regular expression matching correct function names
function-rgx=[a-z_][a-z0-9_]{2,30}$

将那个30接近结束改为40左右.

python 里怎么样通过函数名称来获取函数地址

python 里怎么样通过函数名称来获取函数地址

如果你想通过函数的名称来获取函数的运行地址,可以像下面这样实现:

# File: builtin-import-example-2.py

def getfunctionbyname(module_name, function_name):
module = __import__(module_name)
return getattr(module, function_name)

print( repr(getfunctionbyname("dbm", "open")) )

在这段代码里,通过 open 字符串的名称获取数据库管理模块 dbm 的 open 函数地址。
运行之后,输出如下:

=== RESTART: D:\work\csdn\python_Game1\example\builtin-import-example-2.py ===
<function open at 0x00000226467B2BF8>
>>> 

python 获取调用栈中的函数信息

 

导入 sys 模块,

sys._getframe (i) 表示栈中第 i 层,i=0 表示栈顶

sys.getframe (i).f_code.co_name 表示第 i 层的函数

例如

import treePlotter as tp
import sys

def a():
b()
def b():
c()
def c():
print(sys._getframe(0).f_code.co_name)
print(sys._getframe(1).f_code.co_name)
print(sys._getframe(2).f_code.co_name)
a()
返回值为
D:\python\python.exe F:/python/Tree/two.py
c
b
a

 

Python,名称未定义

Python,名称未定义

这个问题在这里已经有一个答案:> error in python d not defined. 3个
def main()

     name = input ("Please enter your name")

     print ("your name is",name)

在这个程序中(写入python IDLE)当我运行它,并输入我的名字我收到这个错误消息.

Traceback (most recent call last):

 File "C:\Users\********\Documents\PYTHON\hello world.py",line 66,in ?

   main()

  File "C:\Users\********\Documents\PYTHON\hello world.py",line 5,in main

    name = input ("Please enter your name")

  File "<string>",line 0,in ?

NameError: name 'Jim' is not defined

没有定义是什么意思?吉姆是输入,我如何定义输入,而可能是任何字母的混合?

解决方法

2.x中的input()将其输入解释为Python代码.请改用raw_input().并停止将3.x文档应用于2.x.

关于函数名称在python类中未定义函数名称在python类中未定义怎么办的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于NameError:Python 3 中未定义全局名称“xrange”、python – Pylint无效的函数名称、python 里怎么样通过函数名称来获取函数地址、Python,名称未定义等相关知识的信息别忘了在本站进行查找喔。

本文标签: