简体   繁体   English

Python类和命名空间?

[英]Python class and namespace?

lets say I wanna define a class name MyClass. 假设我想定义一个类名MyClass。

and I wanna use that class. 我想要上那堂课。 so, 所以,

class MyClass():

    blabla

MyClass = MyClass(abc)

say I wanna make a function and use MyClass in that function. 说我想创建一个函数并在该函数中使用MyClass。 What I found out is that the following code will through an exception : 我发现以下代码将通过异常:

def MyFunction():
    MyClass = MyClass(abc)

MyFunction(123)

any help? 任何帮助? I can do like 我可以这样做

    Myclass1 = MyClass(abc)

but I think I'm missing something basic about namespaces. 但我认为我遗漏了一些关于命名空间的基本知识。 thank you! 谢谢!

When you execute 当你执行

MyClass = MyClass(abc)

you are creating a new instance of MyClass , and assigning it to the class itself, whereas with Myclass1 = MyClass(abc) , you don't overwrite the class. 您正在创建MyClass的新实例,并将其分配给类本身,而使用Myclass1 = MyClass(abc) ,您不会覆盖该类。 Overwriting the class shouldn't throw an exception (the exception may be to do with calling your MyFunction() method with an argument, since it doesn't take any, or perhaps calling the constructor of MyClass with an argument, since that doesn't take any either), but I imagine it isn't what you intend to do. 覆盖该类不应该抛出异常(异常可能与使用参数调用MyFunction()方法有关,因为它不带任何参数,或者可能用参数调用MyClass的构造函数,因为它没有任何一个),但我想这不是你打算做的。

Myclass1 = MyClass(abc) looks alright to me (besides the argument being passed), although I would recommend using myClass1 (or even better, the generally used python style of my_class1 ) for variables, rather than capitalizing the fist letter, as it prevents confusion about which object is a class. Myclass1 = MyClass(abc)看起来对我好(除了被传递的参数),虽然我建议使用myClass1 (或更好的,通常使用的my_class1 python样式)作为变量,而不是大写第一个字母,因为它可以防止关于哪个对象是一个类的混淆。

MyClass = MyClass(abc)

The above line is the same as doing this: 以上行与执行此操作相同:

def x():
   print 'Hello'
   return 'Hello'

x() # outputs Hello
x = x() # Also ouputs 'Hello' (since x() is called)
x() # Error, since x is now 'Hello' (a string), and you are trying to call it.

In python the name of a variable is just a pointer to a place in memory. 在python中,变量的名称只是指向内存中某个位置的指针。 You can freely assign it to another location (object) by pointing it to something else. 您可以通过将其指向其他位置来自由地将其分配给其他位置(对象)。 In fact, most things you define work that way (like methods and classes). 实际上,您定义的大多数内容都是以这种方式工作的(如方法和类)。 They are just names. 它们只是名字。

Its even stranger if your method doesn't have a return value (like most class __init__ methods). 如果你的方法没有返回值(比如大多数类__init__方法),它甚至更奇怪。 In that case, the left hand side is None , and you'll get TypeError: 'NoneType' object is not callable . 在这种情况下,左侧是None ,您将得到TypeError: 'NoneType' object is not callable Like this: 像这样:

>>> def x():
...   print 'Hello'
...
>>> x()
Hello
>>> x = x()
Hello
>>> x()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>> type(x)
<type 'NoneType'>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM