关于python类(3)property的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于@property装饰器和@[propertyname].getter之间有什么区别?、@prope
关于python 类(3) property的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于@property 装饰器和@[property name].getter 之间有什么区别?、@property 装饰器如何工作? - How does the @property decorator work?、c# – Lambda Expression`x => x.Property`变为`x => Convert(x.Property)`、Discord.py `discord.Guild.roles` 返回 `property object at 0x10d57a720` 或 `'property' not iterable`等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- python 类(3) property
- @property 装饰器和@[property name].getter 之间有什么区别?
- @property 装饰器如何工作? - How does the @property decorator work?
- c# – Lambda Expression`x => x.Property`变为`x => Convert(x.Property)`
- Discord.py `discord.Guild.roles` 返回 `property object at 0x10d57a720` 或 `'property' not iterable`
python 类(3) property
class PetCat(): """ 家猫类""" def __init__(self,name,age): self.name = name # 私有属性 self.__age = age @property def age(self): return self.__age @age.setter def age(self,value): if not isinstance(value,int): print(‘年龄只能是整数‘) return 0 if value < 0 or value > 100: print(‘年龄只能介于0-100之间‘) return 0 self.__age = value # 描述符 @property def show_info(self): return "我叫{0},今年{1}岁".format(self.name,self.age) def __str__(self): # return self.show_info() return ‘---‘if __name__ == ‘__main__‘: cat_black = PetCat(‘小黑‘,2) rest = cat_black.show_info print(rest) # print(‘-------------‘) # print(cat_black) #我叫小黑,今年2岁 cat_black.age = 6 rest = cat_black.show_info print(rest)
@property 装饰器和@[property name].getter 之间有什么区别?
如何解决@property 装饰器和@[property name].getter 之间有什么区别?
@property 装饰器和@[property name].getter 装饰器有什么区别。根据我的理解,除了@[property name].getter 覆盖另一个之外,它们似乎基本相同,例如:
class Class:
def __init__(self,n):
self.__n = n
@property
def n(self):
return self.__n
@n.getter
def n(self):
return self.__n + 3
obj = Class(5)
print(obj.n)
除此之外还有什么有目的的区别吗?
@property 装饰器如何工作? - How does the @property decorator work?
问题:
I would like to understand how the built-in function property
works. 我想了解内置函数 property
工作方式。 What confuses me is that property
can also be used as a decorator, but it only takes arguments when used as a built-in function and not when used as a decorator. 令我感到困惑的是,该 property
还可以用作装饰器,但是仅当用作内置函数时才接受参数,而不能用作装饰器。
This example is from the documentation : 这个例子来自文档 :
class C(object):
def __init__(self):
self._x = None
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I''m the ''x'' property.")
property
''s arguments are getx
, setx
, delx
and a doc string. property
的参数是 getx
, setx
, delx
和 doc 字符串。
In the code below property
is used as decorator. 在下面的代码中, property
用作装饰器。 The object of it is the x
function, but in the code above there is no place for an object function in the arguments. 它的对象是 x
函数,但是在上面的代码中,参数中没有对象函数的位置。
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
"""I''m the ''x'' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
And, how are the x.setter
and x.deleter
decorators created? 而且,如何创建 x.setter
和 x.deleter
装饰器? I am confused. 我很困惑。
解决方案:
参考一: https://stackoom.com/question/1AiMi/property 装饰器如何工作参考二: https://oldbug.net/q/1AiMi/How-does-the-property-decorator-work
c# – Lambda Expression`x => x.Property`变为`x => Convert(x.Property)`
public class Data { public int TestInt { get; set; } public bool TestBool { get; set; } public string TestString { get; set; } public Data() { TestInt = 10; TestBool = true; TestString = "test"; } }
并且是一种扩展方法
public static void Method<T>(this T item,params Expression<Func<T,object>>[] properties) { /* Some stuff */ }
我这样用
Data data = new Data(); data.Method(x => x.TestInt,x => x.TestBool,x => x.TestString);
我的方法< T>确实收到3个属性,但它稍微改为:
properties[0] = x => Convert(x.TestId); properties[1] = x => Convert(x.TestBool); properties[2] = x => x.TestString;
如您所见,TestString部分未更改.我尝试将我的属性更改为params Expression< Func< T,bool>> []和params Expression< Func< T,int>> []并且只传递相应的参数,它工作正常.我理解问题来自转换为对象,但我无法弄明白.
解决方法
在Method中,您可能会获得UnaryExpression
with NodeType
= Convert
.如果是这样,只需检查此表达式的Operand属性.
Discord.py `discord.Guild.roles` 返回 `property object at 0x10d57a720` 或 `'property' not iterable`
如何解决Discord.py `discord.Guild.roles` 返回 `property object at 0x10d57a720` 或 `''property'' not iterable`
discord.py = 版本 | 1.6.0:最新
Python = 版本 | 3.9
我整天都在为这个而头疼。
由于某些原因,在 discord.py 1.6.0 版中,discord.Guild.roles
没有返回一个列表,它声明为 here in the docs。但是一个不可迭代的属性对象。
我试过像元组一样解包 for 循环等,甚至试图通过使用 fetch_roles()
来解决这个问题,但这也是不可迭代的。我只是在这个死胡同。
我的目标是迭代这个列表并找到“基本”。我在公会中的角色。然后使用 @client.event
装饰器将其添加到新用户以检查 on_member_join
。所有这些都适用于向该用户发送消息,但我无法让角色返回角色列表。
感谢任何帮助。
async def on_member_join(member):
try:
server = member.guild.name
user = member.id
roles_list = discord.Guild.roles
# Gets the member role as a `role` object
# should iter over list of roles from above
role = discord.utils.get(roles_list,name="Basic")
# Gives the role to the user
await member.add_roles(role)
...
解决方法
你不应该引用类本身,而是引用实例。
roles_list = server.roles # You defined it above,or `member.guild.roles`
今天关于python 类(3) property的介绍到此结束,谢谢您的阅读,有关@property 装饰器和@[property name].getter 之间有什么区别?、@property 装饰器如何工作? - How does the @property decorator work?、c# – Lambda Expression`x => x.Property`变为`x => Convert(x.Property)`、Discord.py `discord.Guild.roles` 返回 `property object at 0x10d57a720` 或 `'property' not iterable`等更多相关知识的信息可以在本站进行查询。
本文标签: