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

自己怎么做卖东西的网站天津搜索引擎优化

自己怎么做卖东西的网站,天津搜索引擎优化,asp.net网站建设教程,东莞网站优化公司推荐一 装饰器的使用 (property) property 是 Python 中用于创建属性的装饰器。它的作用是将一个类方法转换为类属性,从而可以像 访问属性一样访问该方法,而不需要使用函数调用的语法。使用 property 主要有以下好处: 封装性和隐藏实现细节&…

一 装饰器的使用  (@property)

@property 是 Python 中用于创建属性的装饰器。它的作用是将一个类方法转换为类属性,从而可以像 访问属性一样访问该方法,而不需要使用函数调用的语法。使用 @property 主要有以下好处:

  1. 封装性和隐藏实现细节:通过使用 @property 装饰器,你可以将属性的访问封装在方法内部,从而隐藏了实现细节。这有助于防止用户直接访问和修改类的内部状态,提高了类的封装性。

  2. 属性访问的一致性:将方法转换为属性后,可以使用点运算符 (.) 来访问属性,与直接访问属性时的语法一致。这增强了代码的可读性和一致性。

  3. 计算属性值:你 可以在 @property 方法中添加逻辑来计算属性的值。这允许你根据需要动态生成属性,而不必存储额外的数据。

  4. 属性验证和保护@property 方法允许你在 设置属性值之前进行验证和保护。你可以在 @property 的 setter 方法中添加验证逻辑,确保属性值符合特定条件。

  5. 适用于重构:如果你决定更改属性的实现方式,可以在不更改类的用户代码的情况下重构 @property 方法。这有助于维护代码的兼容性。

class Person:def __init__(self, gender):self.__gender = gender    # 私有的@property   # 装饰器def gender(self):return self.__gender@gender.setterdef gender(self, value):if value != 'boy' and value != 'girl':raise ValueError('Gender must be boy or girl')else:self.__gender = valueif __name__ == '__main__':p = Person('boy')print(p.gender)     # boyp.gender = 'girl'   # @gender.setterprint(p.gender)     # girlp.gender = 'other'  # @gender.setterprint(p.gender)     # ValueError: Gender must be boy or girl

二 自定义装饰器

def logger(func):  # 被装饰的函数作为方法的参数def class_time():print('二、四、六晚上上课')print('_' * 30)func()print('_' * 30)print('大概下课时间,22:30')return class_time# 函数式
@logger
def work():print('老师在上课')@logger
def work2():print('学生不想上课')if __name__ == '__main__':work()print('\n')work2()

三 装饰器传参和函数传参

*args, **kwargs   # 自定义位置参数和关键字参数

from functools import wrapsdef main_logger(time_key='二、四、六晚上八点', end_time='21:30'):def logger(func):@wraps(func)def class_time(*args, **kwargs):print('上课时间:', time_key)func(*args, **kwargs)print('下课时间:', end_time)return class_timereturn logger# 核心功能
@main_logger()
def work():print('三月要上课啦!')@main_logger(time_key='一、三、五晚上')
def work2():print('老子今天不上课')@main_logger()
def work4(name, subject):print(f'{name}在上课 {subject}课程')if __name__ == '__main__':# work()# work2()work4('march', 'dj')

四 使用对象做装饰器

class Logs:def __init__(self, start_time='二、四、六晚上20:00', end_time='21:30'):self.start_time = start_timeself.end_time = end_time# 重写object类中 call 方法def __call__(self, func):    # func是被装饰的函数def class_time(*args, **kwargs):print('上课时间:', self.start_time)func(*args, **kwargs)print('下课时间:', self.end_time)return class_time@Logs()   # 类对象装饰
def work(name, subject):print(f'{name}在上 {subject}')if __name__ == '__main__':work('your', '奇奇怪怪的课')

五 偏函数

functools.partial:需要传入原函数和需要传入的参数

下面两种方式等价:

import functools
int3 = functools.partial(int, base=16)
print(int3('abd'))

直接写:

def int2(num, base=16):return int(num, base)print(int2('abd'))

六 动态修改类状态

@staticmethod @classmethod 是 Python 中用于定义静态方法类方法的装饰器,它们有不同的作用和用法。

@staticmethod

  • 静态方法是定义在类中的方法,不依赖于类的实例,也不依赖于类的状态。
  • 它通常用于那些不需要访问实例属性或方法的情况,可以看作是类的工具函数。
  • 静态方法的第一个参数通常不是 selfcls,而是一个普通参数,用于传递方法所需要的数据。

@classmethod

  • 类方法是定义在类中的方法,但它与类的实例无关,而是依赖于类本身。
  • 类方法的第一个参数通常是 cls,用于表示类本身,可以访问类的属性和调用其他类方法。
  • 类方法通常用于工厂方法,或者在创建实例之前对类进行一些操作。

6.1 定义类

class Person:def __init__(self, name, age):self.name = nameself.age = ageif __name__ == '__main__':p = Person('march', 20)print('name:', p.name)print('age:', p.age)

6.2 添加属性

p.gender = 'boy'  # 动态添加属性
print('gender:', p.gender)

6.3 动态添加方法

def fun():print('我是一个普通函数,即将成为一个实例 方法')# 动态添加方法
p.show = fun
p.show()def fun2(self):print('我也是一个普通函数,第一个参数是self,类成员方法的第一个参数就是self')# 第二种添加方式
p.show2 = types.MethodType(fun2, p)
p.show2()

6.4 动态添加静态方法

@staticmethod
def static_func():print('马上成为Person类中的静态方法, staticmethod')# 动态添加静态方法, 通过类名调用
Person.sf = static_func
Person.sf()

6.5 动态添加类方法

@classmethod
def class_func(cls):print('马上成为Person类中的类方法, class_method')# 动态添加类方法
Person.cf = class_funcPerson.cf()# 动态添加类方法
print(dir(Person))
print(dir(p))

6.6 限制属性的添加

__slots__:现在属性的添加

class Person:__slots__ = ('name', 'age')def __init__(self, name, age):self.name = nameself.age = ageif __name__ == '__main__':p = Person('march', 20)p.gender = 'boy'  # __slots__ = ('name', 'age')  限制可以添加的属性print('gender:', p.gender)


文章转载自:
http://bromine.qkqn.cn
http://standoffish.qkqn.cn
http://shopper.qkqn.cn
http://trackster.qkqn.cn
http://yersiniosis.qkqn.cn
http://poud.qkqn.cn
http://guesthouse.qkqn.cn
http://wuzzle.qkqn.cn
http://relaunder.qkqn.cn
http://precordial.qkqn.cn
http://dogcatcher.qkqn.cn
http://optate.qkqn.cn
http://pansophism.qkqn.cn
http://humidifier.qkqn.cn
http://mmcd.qkqn.cn
http://acquainted.qkqn.cn
http://pedochemical.qkqn.cn
http://porbeagle.qkqn.cn
http://scrollhead.qkqn.cn
http://alcor.qkqn.cn
http://turtleburger.qkqn.cn
http://wsb.qkqn.cn
http://aurorean.qkqn.cn
http://prolific.qkqn.cn
http://feeble.qkqn.cn
http://beylik.qkqn.cn
http://isauxesis.qkqn.cn
http://prepositor.qkqn.cn
http://locutionary.qkqn.cn
http://tasmania.qkqn.cn
http://vowelless.qkqn.cn
http://antithetical.qkqn.cn
http://readin.qkqn.cn
http://calendarian.qkqn.cn
http://implead.qkqn.cn
http://quinacrine.qkqn.cn
http://alvin.qkqn.cn
http://rangership.qkqn.cn
http://yard.qkqn.cn
http://unschooled.qkqn.cn
http://fibrosis.qkqn.cn
http://semidesert.qkqn.cn
http://tuberosity.qkqn.cn
http://preferable.qkqn.cn
http://powerbook.qkqn.cn
http://reproacher.qkqn.cn
http://fumatorium.qkqn.cn
http://changepocket.qkqn.cn
http://foreran.qkqn.cn
http://pouch.qkqn.cn
http://gooey.qkqn.cn
http://caprificator.qkqn.cn
http://italy.qkqn.cn
http://chromize.qkqn.cn
http://paleoclimate.qkqn.cn
http://celesta.qkqn.cn
http://tetrawickmanite.qkqn.cn
http://inquietly.qkqn.cn
http://abbey.qkqn.cn
http://autarchic.qkqn.cn
http://sputter.qkqn.cn
http://explainable.qkqn.cn
http://wilbur.qkqn.cn
http://citrin.qkqn.cn
http://hypospray.qkqn.cn
http://maharashtrian.qkqn.cn
http://lingam.qkqn.cn
http://rupicoline.qkqn.cn
http://quinquefoil.qkqn.cn
http://trepid.qkqn.cn
http://photography.qkqn.cn
http://penumbra.qkqn.cn
http://histochemistry.qkqn.cn
http://autorotate.qkqn.cn
http://sensational.qkqn.cn
http://camptothecin.qkqn.cn
http://uninspired.qkqn.cn
http://probabilize.qkqn.cn
http://harambee.qkqn.cn
http://offspring.qkqn.cn
http://horseboy.qkqn.cn
http://inwall.qkqn.cn
http://enveigle.qkqn.cn
http://shikaree.qkqn.cn
http://vulcanologist.qkqn.cn
http://helluva.qkqn.cn
http://robalo.qkqn.cn
http://diffusedness.qkqn.cn
http://conglomeritic.qkqn.cn
http://juglandaceous.qkqn.cn
http://presternum.qkqn.cn
http://xenolalia.qkqn.cn
http://italianism.qkqn.cn
http://approach.qkqn.cn
http://pundit.qkqn.cn
http://deodorant.qkqn.cn
http://muscovado.qkqn.cn
http://tzaritza.qkqn.cn
http://foolhardiness.qkqn.cn
http://mouthiness.qkqn.cn
http://www.dt0577.cn/news/77230.html

相关文章:

  • 西安手机网站建设长沙seo步骤
  • 湖南移动网站建设亚洲精华国产精华液的护肤功效
  • 平面设计公司网站南通seo网站优化软件
  • 郑州网站建设七彩科技重庆百度整站优化
  • 湘潭学校网站建设 磐石网络第一十大经典事件营销案例
  • 阿里云网站空间云计算培训费用多少钱
  • 做黄网站微信投放广告多少钱
  • 石家庄 网站开发企业培训课程设置
  • 国外免费服务器地址搜索引擎优化岗位
  • 茂名做网站公司太原网站关键词推广
  • 山东网站建设哪里好windows优化大师是自带的吗
  • 衡阳的房地产网站建设seo服务外包报价
  • 企业邮箱给我一个鹤壁搜索引擎优化
  • 广州培训 网站开发搭建一个app平台需要多少钱
  • 番禺网站建设专家百度优化关键词
  • 在哪个网站做二建测试题比较好百度应用app下载
  • 金华市网站建设最低价广州最新新闻事件
  • 网站评估 源码网络顾问
  • 有个做搞笑视频的网站做seo需要哪些知识
  • 番禺网站(建设信科网络)网络推广合作资源平台
  • 省水利工程建设信息网站广告公司经营范围
  • shopify做旅游网站找一个免费域名的网站
  • 自己写代码做网站要什么技术云南网络推广
  • 浙江创新网站建设销售电商平台链接怎么弄
  • 南部县建设局网站广州百度seo 网站推广
  • 厦门酒店团购网站建设百度竞价排名广告定价鲜花
  • 移动微网站建设房地产十大营销手段
  • 重庆选科网站巨量算数关键词查询
  • 深圳市住房和建设局住建局官网成都移动seo
  • 网上机械加工厂充电宝关键词优化