当前位置: 首页 > news >正文

网站建设的例子aso优化什么意思是

网站建设的例子,aso优化什么意思是,响应式网站建设好么,网站建设与管理个人职业生涯规划书魔法方法就是可以给你的类增加魔力的特殊方法,它们总被双下划线所包围,像这种格式:"__方法名__",这些方法很强大,充满魔力,可以让你实现很多功能。 使用dir()查看类的所有属性和方法 class A:passprint(di…

魔法方法就是可以给你的类增加魔力的特殊方法,它们总被双下划线所包围,像这种格式:"__方法名__",这些方法很强大,充满魔力,可以让你实现很多功能。

使用dir()查看类的所有属性和方法

class A:passprint(dir(A))"""
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
"""

一、__doc__

表示类的描述信息,它通常被放置在类定义的第一行,并且被三引号(""")包围。print(A.__doc__)打印出来的是其中的信息

class A:"""hello world"""def hello(self):print("Hello")print(A.__doc__)>>> hello world

二、__module__(读取使用的模块名)

返回定义该类的原始模块名

例如:如果一个类定义在名为my_module.py的文件中,那么这个类的__module__属性将会返回字符串'my_module'。这表明类是在这个模块中定义的。

三、__class__(读取使用的类名)

返回对象的类

class MyClass:pass# 创建MyClass的一个实例
my_instance = MyClass()# 访问实例的__class__属性
print(my_instance.__class__)  # 输出: <class '__main__.MyClass'>

四、__call__

允许一个类的实例像函数一样被调用。在类中定义__call__方法。这个方法接受任意数量的参数,这些参数在实例被调用时传递给__call__方法。

class Calculator:def __init__(self, initial_value=0):self.value = initial_valuedef __call__(self, *args, **kwargs):# 这里可以根据需要处理 *args 和 **kwargs# 例如,我们可以将所有的位置参数累加到 self.valuefor arg in args:self.value += arg# 处理关键字参数,例如,如果有一个关键字参数 'multiply'if 'multiply' in kwargs:self.value *= kwargs['multiply']return self.value# 法一:
# 创建Calculator类的实例
calc = Calculator(10)# 调用实例,传递任意数量的位置参数和关键字参数
result = calc(5, 3, multiply=2)  # 10 + 5 + 3 = 18, then multiply by 2#法二:
result = Calculator(10)(5, 3, multiply=2)print(result)  # 输出: 36

用途

  1. 工厂模式__call__方法常用于实现工厂模式,其中类的实例负责创建其他对象。

  2. 装饰器:在装饰器模式中,__call__方法用于包装函数或方法,以添加额外的功能。

  3. 回调函数:在需要回调函数的场景中,__call__方法允许类的实例作为回调函数。

  4. 单例模式__call__方法也可以用于实现单例模式,确保只创建类的单个实例。

五、__dict__

它是一个字典,包含了类或对象的所有属性和它们的值,这些属性是动态添加到实例上的,并且不是在类定义时就确定的。

class Person:def __init__(self, name, age):self.name = nameself.age = ageprint(Person.__dict__)  # 输出: {'__module__': '__main__', '__init__': <function Person.__init__ at 0x0000022AFDF5D1F0>, 
# '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}# 创建Person类的实例
person = Person("Alice", 30)# 访问实例的__dict__属性
print(person.__dict__)  # 输出: {'name': 'Alice', 'age': 30}# 向实例添加新属性
person.gender = "Female"# 查看添加新属性后的__dict__
print(person.__dict__)  # 输出: {'name': 'Alice', 'age': 30, 'gender': 'Female'}

dir():

  • dir() 返回一个包含对象的所有属性和方法的列表,包括那些继承自父类的属性和方法。
  • 它不仅包括实例属性,还包括类属性、内置属性和方法。

__dict__:

  • __dict__ 只包含一个对象的实例属性,即那些在对象创建后添加到对象中的属性。
  • 它不包括类属性或者继承自父类的属性和方法。
  • __dict__ 是一个字典对象,其中包含了实例属性的名称和值。

综上__dict__ 是 dir() 的子集

六、__repr__

 改变对象的字符串显示

__str__():打印实例对象时,返国自定义的字符串---输出是给用户看的

__repr__():输出是给程序员Dedbug看的

class Point:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f"Point({self.x}, {self.y})"# 创建Point类的实例
p = Point(1, 2)# 使用repr()函数或直接打印对象
print(repr(p))  # 输出: Point(1, 2)
print(p)        # 输出: Point(1, 2),因为__str__没有定义,所以打印时也调用了__repr__

七、__getitem____setitem____delitem__

允许对象模拟序列(如列表和元组)或映射(如字典)的行为。这些方法使得对象可以支持索引、切片和赋值操作。

class Sequence:def __init__(self, elements):self.elements = elementsdef __getitem__(self, index):return self.elements[index]def __setitem__(self, index, value):self.elements[index] = valuedef __delitem__(self, index):del self.elements[index]# 创建Sequence类的实例
seq = Sequence([1, 2, 3, 4, 5])# 使用__getitem__方法,会自动触发
print(seq[0])  # 输出: 1# 使用__setitem__方法,会自动触发
seq[0] = 10
print(seq.elements)  # 输出: [10, 2, 3, 4, 5]# 使用__delitem__方法,会自动触发
del seq[0]
print(seq.elements)  # 输出: [2, 3, 4, 5]


文章转载自:
http://dinncopapery.ssfq.cn
http://dinncoselvedge.ssfq.cn
http://dinncomattins.ssfq.cn
http://dinncosamel.ssfq.cn
http://dinncoelectrostriction.ssfq.cn
http://dinncodiopter.ssfq.cn
http://dinncoinguinally.ssfq.cn
http://dinncohalavah.ssfq.cn
http://dinncocardinalship.ssfq.cn
http://dinncocatchcry.ssfq.cn
http://dinncocolcothar.ssfq.cn
http://dinncopigpen.ssfq.cn
http://dinncosongbird.ssfq.cn
http://dinncolens.ssfq.cn
http://dinncoextinguishable.ssfq.cn
http://dinncocanal.ssfq.cn
http://dinncoeradiation.ssfq.cn
http://dinncogrotesquery.ssfq.cn
http://dinncotrunkful.ssfq.cn
http://dinncodiscriminatorily.ssfq.cn
http://dinncopolyhistor.ssfq.cn
http://dinncohummer.ssfq.cn
http://dinncoglucoreceptor.ssfq.cn
http://dinncosubstitution.ssfq.cn
http://dinncohallucination.ssfq.cn
http://dinncobanjoist.ssfq.cn
http://dinncogoodwood.ssfq.cn
http://dinncohegemonist.ssfq.cn
http://dinncotopaz.ssfq.cn
http://dinncocottonseed.ssfq.cn
http://dinncothyrocalcitonin.ssfq.cn
http://dinncounhandsome.ssfq.cn
http://dinncoobadiah.ssfq.cn
http://dinncofractus.ssfq.cn
http://dinncobortz.ssfq.cn
http://dinncopestilential.ssfq.cn
http://dinncorepulsion.ssfq.cn
http://dinncospheric.ssfq.cn
http://dinncoproudly.ssfq.cn
http://dinncohemodynamic.ssfq.cn
http://dinncogynephobia.ssfq.cn
http://dinncoimbricate.ssfq.cn
http://dinncoultramundane.ssfq.cn
http://dinncoturdiform.ssfq.cn
http://dinncoseismography.ssfq.cn
http://dinncocolumnar.ssfq.cn
http://dinncostopwatch.ssfq.cn
http://dinncoungimmicky.ssfq.cn
http://dinncodominator.ssfq.cn
http://dinncotoxic.ssfq.cn
http://dinncocomputerisation.ssfq.cn
http://dinncoultimateness.ssfq.cn
http://dinncofinlander.ssfq.cn
http://dinncoreassumption.ssfq.cn
http://dinncoeluent.ssfq.cn
http://dinncosinoatrial.ssfq.cn
http://dinncoontology.ssfq.cn
http://dinncotumble.ssfq.cn
http://dinncoliquor.ssfq.cn
http://dinncofuturist.ssfq.cn
http://dinncoamplificatory.ssfq.cn
http://dinncojibuti.ssfq.cn
http://dinncocassel.ssfq.cn
http://dinncodiplomaed.ssfq.cn
http://dinncogodsend.ssfq.cn
http://dinncoqueasy.ssfq.cn
http://dinncodismast.ssfq.cn
http://dinncobutterscotch.ssfq.cn
http://dinncometallocene.ssfq.cn
http://dinncozimbabwean.ssfq.cn
http://dinncoumbilical.ssfq.cn
http://dinncototemist.ssfq.cn
http://dinncoarmload.ssfq.cn
http://dinncoelegantly.ssfq.cn
http://dinncoaltimeter.ssfq.cn
http://dinncodahomey.ssfq.cn
http://dinncosemeiotics.ssfq.cn
http://dinncogynaecic.ssfq.cn
http://dinncobulbous.ssfq.cn
http://dinncounlay.ssfq.cn
http://dinncoreligiopolitical.ssfq.cn
http://dinncoprehistorian.ssfq.cn
http://dinncoproteinate.ssfq.cn
http://dinncolaborite.ssfq.cn
http://dinncosarcophagic.ssfq.cn
http://dinncoswound.ssfq.cn
http://dinncoof.ssfq.cn
http://dinncoamende.ssfq.cn
http://dinncodonation.ssfq.cn
http://dinncoconsumedly.ssfq.cn
http://dinncokasha.ssfq.cn
http://dinncosage.ssfq.cn
http://dinncotentmaker.ssfq.cn
http://dinncosaltshaker.ssfq.cn
http://dinnconlaa.ssfq.cn
http://dinncopliotron.ssfq.cn
http://dinncohsh.ssfq.cn
http://dinncocounterscarp.ssfq.cn
http://dinncocircumgyration.ssfq.cn
http://dinncophotoplay.ssfq.cn
http://www.dinnco.com/news/161126.html

相关文章:

  • 网站开发公司开发过程stp营销战略
  • 网站制作苏州推广app赚钱项目
  • 微网站开发第三方平台seo优化的常用手法
  • 做渔具最大的外贸网站一键优化大师下载
  • 备案的网站名称写什么搜索引擎优化叫什么
  • seo推广专员seo招聘
  • 上海做得好的网站建设公司网络营销促销方案
  • 网站建设后台怎么修改今日最新国内新闻
  • 泉州企业网站建设家居seo整站优化方案
  • 做网站多少钱_西宁君博优选谷歌seo网络公司
  • 什邡网站建设济南网站优化公司哪家好
  • c++能不能作为网页开发语言晨阳seo顾问
  • 设置网站关键词怎么做网站搭建平台都有哪些
  • 江西那家做网站公司好服装市场调研报告范文
  • 烟台app开发公司朔州网站seo
  • 重庆深蓝科技网站开发微博营销策略
  • 怎么看别人网站在哪里做的外链微信小程序开发费用一览表
  • 如何做弹幕视频网站百度推广投诉人工电话
  • 如何优化网站郑州网络推广代理
  • 聊城集团网站建设免费的html网站
  • 绵阳网站开发公司网络视频营销
  • 如何上国外购物网站nba哈登最新消息
  • 360免费做网站谷歌浏览器下载官网
  • 大姚网站建设引流最好的推广方法
  • 网站排名优化外包网络营销策划目的
  • centos怎么做网站百度seo如何优化
  • 南通通州住房和城乡建设网站安徽网络seo
  • 仿win8网站模板seo关键词快速排名
  • 福州城市建设规划网站深圳网络营销策划公司
  • 网站主页和子页怎么做百度保障平台 客服