什么是函数什么是方法,类其中@property装饰器的使用
2021/8/12 6:36:38
本文主要是介绍什么是函数什么是方法,类其中@property装饰器的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
什么是函数,什么是方法
假设存在类
class Foo():
def get(self):
pass
Foo.fetch(123)
上述如果直接通过类来调用类其中的get函数,那这个就是函数
然而 如果
obj = Foo()
obj.get()
通过类实例化成一个对象来调用类中的方法就是方法,总结 通过类直接调用的方法,需要自己手动传参的,那是函数,通过对象调用的那是方法
补充:类里的私有字段的调用
class Foo():
def __init__(self):
self.__age=18
def get_age(self):
print(self.__age)
obj = Foo()
obj.age不能z直接调用age字段
第一种方法:age = obj.get_age() #在类内部设置一个函数来取,在类内部可以取
第二种方法:age = obj._Foo__age #强取私有字段
第三块也是最重要的一块:
假设存在这样一个函数
class User(): @property def password(self): raise AttributeError("不可读") #密码只能赋值 并不能读,如果能够读取会很不安全 @password.setter def password(self,passwd): self.password_hash = generate_password_hash(passwd) user = User() print(user.password) #则会执行@property下的password方法 user.password = "xxxxx" #则会执行@password.setter的方法 #给类设置虚拟属性 而上面就是属性的存(@password.setter)和取(@property)
还有补充
mysql调用 net start mysql
然后 mysql -u root -p
输密码即可
然后 show databases;
然后 use xxxbase;
插入数值什么
insert xxxtable(字段1,字段2,字段3) values("字段值1",“字段值2”,“字段值3”)
这篇关于什么是函数什么是方法,类其中@property装饰器的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南