换季7914 发表于 2019-4-20 15:13:18

太详细了。感谢感谢

葫芦爷救娃娃 发表于 2019-4-29 16:06:43

逐渐理解

liugan 发表于 2019-5-21 17:08:48

真的好使

z3m4 发表于 2019-5-23 11:28:28

#给长方形的长和宽赋值时自动进行形状检测,改变shape属性
class Rectangle:

    def __init__(self,w=0,h=0):
      self.width=w
      self.height=h
      if w==h:
            self.shape='正方形'
      else:
            self.shape='长方形'

    def __setattr__(self,name,value):
      self.__dict__= value
      if self.width==self.height:
            self.shape='正方形'
      else:
            self.shape='长方形'

    def getArea(self):
      return self.width*self.height

尝试了很多方法,都无法调通,求高手指导!

z3m4 发表于 2019-5-23 13:31:38

#给长方形的长和宽赋值时自动进行形状检测,改变shape属性
class Rectangle:

    def __init__(self,w=0,h=0):
      self.width=w
      self.height=h
      if w==h:
            self.shape='正方形'
      else:
            self.shape='长方形'

    def __setattr__(self,name,value):
      self.__dict__= value
      try:
            if self.width==self.height:
                self.__dict__['shape']='正方形'
            else:
                self.__dict__['shape']='长方形'
      except AttributeError:
            self.__dict__['height']=-1

    def getArea(self):
      return self.width*self.height

解决掉了,✌

zhxhwyx 发表于 2019-6-13 15:23:49

{:10_297:}学习

TheOne000 发表于 2019-6-19 13:44:29

__repr__(self)        定义当被 repr() 调用时的行为
__str__(self)        定义当被 str() 调用时的行为
这是啥意思啊?就是类被调用时,调用这个方法吗?

给你讲个笑话 发表于 2019-7-9 08:42:14

我有个问题啊,当你重新定一个反运算的时候,比如:
class Nint(int):
        def __radd__(self,other):
                return int.__sub__(self,other)

a=Nint(3)
1+a
2
1+2
3
为什么当是数字跟数字相加的时候,那个+没有被改变

972728330 发表于 2019-7-25 10:12:02

如果能加上应用实例就更好啦

lqw609659337 发表于 2019-7-25 22:43:08

留着
{:5_109:}

CZ5CZ 发表于 2019-8-4 19:50:06

加油!大家一起加油!

Mr.南北 发表于 2019-8-14 14:52:12

努力学习打卡

我有酒两杯 发表于 2019-9-17 10:59:34

xieixe

anfree 发表于 2019-9-29 21:39:51

努力學習中~

zyloong 发表于 2019-10-12 11:08:28

真.灵活即强大

OnlyFive 发表于 2019-10-23 17:34:33

打卡,不明觉厉

iRobin 发表于 2019-12-25 17:11:07

顶贴表示下支持

qsxr 发表于 2020-1-3 10:56:33


一元操作符里面的
__pos__(self)        定义正号的行为:+x
__neg__(self)        定义负号的行为:-x

跟视频上的含义不一样,,,到底哪个才是对的哟???

bo20000000 发表于 2020-1-16 13:39:15

慢慢的就看不懂了 越来越复杂 内置的还能改

此帐号已被注销 发表于 2020-2-6 20:18:13

太多了{:10_249:}
页: 1 2 3 4 5 6 7 [8] 9 10 11
查看完整版本: Python 魔法方法详解