返回顶部

收藏

python类的成员变量定义

更多
class FirstClass:                 # define a class object
     def setdata(self, value):    # define class methods
         self.data = value        # self is the instance
     def display(self):
         print self.data          # self.data: per instance

x = FirstClass()                  # make two instances
y = FirstClass()                  # each is a new namespace

x.setdata('Data')                 # call methods: self is x
y.setdata(3.14159)                # runs: FirstClass.setdata(y, 3.14159)

x.display()                       # self.data differs in each

y.display()

x.data = 'New value'              # can get/set attributes 
x.display()                       # outside the class too

标签:Python,Class

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2015-03-23 16:00:44python构造函数 by wubai
  2. 2015-04-12 12:23:16Demonstrates class attributes and static methods by Solon.Ring
  3. 2015-04-26 09:00:43python魔幻方法:__str__ , __repr__ by Solon.Ring
  4. 2015-03-25 09:01:36输出对象属性 by wubai
  5. 2015-04-04 09:20:00Print out class tree by 大猪
  6. 2015-05-04 09:28:31python对象声明 by wubai
  7. 2015-04-04 09:41:16python面向对象:__del__函数使用 by jack.chen
  8. 2015-04-13 09:39:34重写内置的__str__方法 by wubai
  9. 2015-05-05 14:53:44python自定义list by 阮小七
  10. 2015-05-09 17:34:43Python定义类 by Solon.Ring
  11. 2019-02-21 11:47:08Thinkphp5整合微信扫码支付实例 by 好小灰灰

发表评论