简体   繁体   English

在函数外部实例化的对象。 我如何从功能访问?

[英]An object instantiated outside of a function. How can I access from the fuction?

Tried to access as argument of the function.试图作为函数的参数访问。 Not working!不工作!

object = class()

def foo(object):
    object.method

foo()

Not working!不工作!

I am going to be referring to object as obj since you should not be overriding built-in classes, especially since its a new-style class.我将把object称为obj因为您不应该覆盖内置类,特别是因为它是一个新型类。

Simply put, obj within the function definition is a parameter name, not the variable obj you have assigned.简单地说,函数定义中的obj是一个参数名称,而不是您分配的变量obj

Therefore, when calling the function foo , you need to provide it the parameter it asks for.因此,在调用函数foo ,您需要为其提供所需的参数。 So your code should be like this:所以你的代码应该是这样的:

foo(obj)

not foo()不是foo()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择具有 class 功能的 function? - How can I choose function with class fuction? 如何从线程函数外部访问对象 - How to access an object from outside a threaded function 如何修补在 function 之外实例化的 object,以便在每个测试 python 中重复使用 - How to patch an object instantiated outside of a function to reuse in every test python 如何在函数中定义参数。 - How can I define a paramter in a function. 函数值检索:如何在函数外部访问变量的值。 以及如何在另一个函数中使用该值? - Function Value Retrieval: how to access the value of a variable outside a function. and how use that value in another function? 如何模拟在构造函数中实例化的对象? - How can I mock an object instantiated in the constructor? 如何访问在函数外部创建的特定变量。 即使该函数中有多个其他变量 - How to access a specific variable created inside a function outside a function. Even when there are multiple other variables in that function 列表可以在 class 内部和 function 外部创建,但仍然可以从 function 内部访问。 但不是变量 - List can be created inside a class and outside a function, but can still be accessed from inside of that function. But not a variable 如何在 function 中定义 class,然后在 ZA7F425268E68385D1AB5074C17A94F14Z 中从外部访问它 - How can I definine a class inside a function, then access it from outside said function in Python? 使用可变默认参数,如何从函数外部访问对象? - Using a mutable default argument, how to access the object from outside the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM