1.在函数外部获取函数名称,用func.__name__获取 2.在函数内部获取当前函数名称,用sys._getframe().f_code.co_name方法获取 3.使用inspect模块动态获取当前运行的函数名,return inspect.stack()[1][3]需要封装个方法在被测函数中调用 4.在类内部获取类名称self.__class__.__name__ importinspect,sysdeftes...
def hello(self): print('the name of method is ## {} ##'.format(sys._getframe().f_code.co_name)) print('the name of class is ## {} ##'.format(self.__class__.__name__))if__name__ =="__main__": h=Hello() h.hello() 运行结果: the name of methodis## hello ## t...
print('%s的生命值减少%s,剩余%s'%(,self.aggr,dog.hp)) class Person: # 类名 Person role = '人' # 类的静态变量 是所有的对象共享的一个属性 def __init__(self,name,sex,aggr,hp): #方法 动态属性 内置的双下方法 = name # 对象属性 实例属性 self.sex = sex self.aggr = aggr self.hp ...
class Vehicle: def name(self, name): return name v = Vehicle() print(type(v).__name__) Run Code Output Vehicle Using attribute __name__ with type(), you can get the class name of an instance/object as shown in the example above. type() gives the class of object v and __nam...
soup=BeautifulSoup(html,'lxml')print("豆瓣电影250:序号 \t 影片名 \t 评分 \t 评价人数")fortaginsoup.find_all(attrs={"class":"item"}):content=tag.get_text()content=content.replace('\n','')# 删除多余换行print(content,'\n')# 主函数if__name__=='__main__':url='https://movie....
from xml.etreeimportElementTreeasETimportjson tree=ET.parse('./resource/movie.xml')root=tree.getroot()all_data=[]formovieinroot:# 存储电影数据的字典 movie_data={}# 存储属性的字典 attr_data={}<spanclass="hljs-comment"># 取出 type 标签的值</span>movie_type=movie.find(<spanclass="hljs...
The only difference is that you’re using cls instead of func as the parameter name to indicate that it’s meant to be a class decorator. Check it out in practice: Python >>> from decorators import singleton >>> @singleton ... class TheOne: ... pass ... >>> first_one = ...
class Person: def __init__(self, name, age): self.name = name self.age = age def say_hello(self): print("Hello, my name is", self.name) 我们定义了一个名为Person的类,它具有两个属性name和age,以及一个方法say_hello。__init__方法是一个特殊的方法,用于初始化对象的属性。self参数是一...
class A(object):"""模块中的自定义类A"""def __init__(self, name):self.name = name def get_name(self):"返回类的实例的名称"return self.name instance_of_a = A('一个实例')class B(A):"""这是类B 它继承自A类."""# 这个方法是B类独有的方法.def do_something(self):"""B类的实例...
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就具有了被对象调用的能力 ...