这种方法简单直观,但需要在每个类的构造函数中手动设置类名。 使用__class__.__name__ 另一种方法是直接使用__class__.__name__属性。这是Python中获取当前对象类名的标准方式。 classMyClass:defget_class_name(self):returnself.__class__.__name__# 使用示例obj=MyClass()print(obj.get_class_name(...
classMyClass:def__init__(self): self.name ="Tom"obj = MyClass()print(obj.getclass())# 输出:MyClass 在这个示例中,我们定义了一个名为MyClass的类,并创建了一个该类的对象obj。然后,我们通过调用obj.getclass()函数获取了该对象的类名,结果为"MyClass"。 需要注意的是,除了getclassname()函数之外...
1. 语法 self.__class__.__name__# class namecls.__name__# class namesys._getframe().f_code.co_filename# 当前文件名, 或通过__file__获取sys._getframe().f_code.co_name# 当前函数名sys._getframe().f_lineno# 当前行号 2. 例子 importsysclassFoo(object):defprint_name(self):print(...
self.name=name self.author=author class Book2(Book1): def __init__(self, name, author, press): super(Book2, self).__init__(name, author) self.press=press #子类Book2在继承时新增参数press class Book3(Book2): def __init__(self, name, author, press, totalpage): super(Book3, se...
class people: __name = 'jack' __age = 12 def getName(self): return self.__name def getAge(self): return self.__age p = people() print p.getName(),p.getAge() 如果对self不好理解的话,可以把它当做C++中类里面的this指针一样理解,就是对象自身的意思,在用某个对象调用该方法时,就将...
class Company: def Company_name(self, name): self.name = name # self初始化变量name,让name的作用域扩展至class内 print(f'The company`s name is {name}') def company_open(self): print(f'{self.name} open') # self.name就具有了被对象调用的能力 ...
: hello""" 下面是一个学生类定义 """classstudent(object):"""这里是一个学生类定义"""def__init__(self, name): self.name = name defget_name(self):return self.name defset_name(self, name): self.name = name defstudy(self):print(f"{ self.name} : 好好学习,天天向上...
The Context class has the following string attributes:Expand table AttributeDescription function_directory The directory in which the function is running. function_name The name of the function. invocation_id The ID of the current function invocation. thread_local_storage The thread local storage of...
get_class() # Get the class name of the window win.get_minmax() # Get the min/max status win.get_process_name() # Get the process name (e.g., "notepad.exe") win.process_name # Property; same as `.get_process_name()` above win.is_always_on_top() # Whether the window has...
class MyClass: def __init__(self, a): self.a = a def __str__(self): return str(self.a) def __repr__(self): class_name = self.__class__.__name__ return f'{class_name}({self.a!r})' @classmethod def get_class_name(cls): return cls.__name__ Return value of str()...