简体   繁体   English

在不使用Python中的类名的情况下创建类的实例

[英]Creating instance of class without using class name in Python

Is it possible to create a class that can be instantiated without using the actual name of said class? 是否可以创建一个无需使用该类的实际名称即可实例化的类? For instance when we make a string, we are instantiating the 'String' class but we aren't using the word 'String' while doing so. 例如,当我们创建一个字符串时,我们将实例化“ String”类,但在执行此操作时并未使用单词“ String”。 eg 例如

example = "Hello, World!"

If it is possible to create such a class, how would one go about doing so? 如果可以创建这样的类,那么如何去做呢?

There is no way to instantiate an object without defining what it is you are attempting to build. 如果不定义您要构建的对象,就无法实例化该对象。 A class (object) is simply a representation of a program stored in memory. 类(对象)只是存储在内存中的程序的表示。 Similar to a variable, the object name defines where that object is stored in memory and how to access that data from memory. 与变量类似,对象名称定义该对象在内存中的存储位置以及如何从内存访问该数据。 You can alias the object within your current program by simply assigning a new variable name to instantiate the object, but you do have to call the object out in order to work with it. 您可以通过简单地分配一个新的变量名来实例化该对象,从而在当前程序中为该对象起别名,但是您必须调出该对象才能使用它。

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

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