类属性

1.类属性

类属性又称为静态变量,或者是静态数据。这些数据是与它们所属的类对象绑定的,不依赖于任何类实例。

2.增 删 改 查

class zoo:
country = 'china'
def __init__(self,name,address,kind):
self.name = name
self.address = address
self.kind = kind
def monkey(self):
print('this is monkey (%s)' %self.address)
def tiger(self):
print('this is tiger (%s)' %self.kind)
def end(self):
print('welcome to %s' %self.name)
dwy = zoo('chinese\'s zoo','beijing','others') zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy) # 对类的数据属性增 删 改 查 print(zoo.country) # 查看信息 zoo.country = 'US' # 修改信息
print(zoo.country) zoo.snake = 'this is snake' # 增加
print(dwy.snake) del zoo.country # 删除
del zoo.snake print(zoo.__dict__)

运行结果:

this is monkey (beijing)
this is tiger (others)
welcome to chinese's zoo
china
US
this is snake
{'__module__': '__main__', '__init__': <function zoo.__init__ at 0x7fb30efb8d90>, 'monkey': <function zoo.monkey at 0x7fb30efb8f28>, 'tiger': <function zoo.tiger at 0x7fb30efc9048>, 'end': <function zoo.end at 0x7fb30efc9378>, '__dict__': <attribute '__dict__' of 'zoo' objects>, '__weakref__': <attribute '__weakref__' of 'zoo' objects>, '__doc__': None} Process finished with exit code 0

3.

class zoo:
country = 'china'
def __init__(self,name,address,kind):
self.name = name
self.address = address
self.kind = kind
def monkey(self):
print('this is monkey (%s)' %self.address)
def tiger(self):
print('this is tiger (%s)' %self.kind)
def end(self):
print('welcome to %s' %self.name)
dwy = zoo('chinese\'s zoo','beijing','others') # 调用
zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy) # 类的函数属性的增加 def eat(self,food):
print('%s 正在吃%s'%(self.name,food))
zoo.eat = eat
print(zoo.__dict__) # 以列表的方式 查看是否添加进去了 dwy.eat('草')

运行结果:

this is monkey (beijing)
this is tiger (others)
welcome to chinese's zoo
{'__module__': '__main__', 'country': 'china', '__init__': <function zoo.__init__ at 0x7f90cdc3ed90>, 'monkey': <function zoo.monkey at 0x7f90cdc3ef28>, 'tiger': <function zoo.tiger at 0x7f90cdc4f048>, 'end': <function zoo.end at 0x7f90cdc4f378>, '__dict__': <attribute '__dict__' of 'zoo' objects>, '__weakref__': <attribute '__weakref__' of 'zoo' objects>, '__doc__': None, 'eat': <function eat at 0x7f90cdd49268>}
chinese's zoo 正在吃草 Process finished with exit code 0

python学习-62 类属性的增 删 该 查的更多相关文章

  1. Python学习:类和实例

    Python学习:类和实例 本文作者: 玄魂工作室--热热的蚂蚁 类,在学习面向对象我们可以把类当成一种规范,这个思想就我个人的体会,感觉很重要,除了封装的功能外,类作为一种规范,我们自己可以定制的规 ...

  2. python之类与对象属性的增删改查

    类属性与对象属性的增删改查 类属性的增删改查 class School: """ 文档 """ Teacher = "老王&quo ...

  3. Python内置类属性,元类研究

    Python内置类属性 我觉得一切都是对象,对象和元类对象,类对象其实都是一样的,我在最后进行了证明,但是只能证明一半,最后由于元类的父类是type,他可以阻挡对object属性的访问,告终 __di ...

  4. Python内置类属性

    __dict__ : 类的属性(包含一个字典,由类的数据属性组成) __doc__ :类的文档字符串 __name__: 类名 __module__: 类定义所在的模块(类的全名是'__main__. ...

  5. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  6. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  7. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  8. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  9. Python学习---django之ORM的增删改查180125

    模型常用的字段类型参数 <1> CharField        #字符串字段, 用于较短的字符串.        #CharField 要求必须有一个参数 maxlength, 用于从数 ...

随机推荐

  1. LeetCode 826. Most Profit Assigning Work

    原题链接在这里:https://leetcode.com/problems/most-profit-assigning-work/ 题目: We have jobs: difficulty[i] is ...

  2. vue关于keep-alive的小坑

    在移动端里 少不了底部导航 在做底部导航的时候点击都会重复请求 我就使用了keep-alive来缓存 每次点击的时候走缓存 这里还有个用途就是当有列表的时候点进详情在返回可以保存之前的滚动记录 不会刷 ...

  3. Educational Codeforces Round 67

    Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...

  4. 洛谷 P3884 [JLOI2009]二叉树问题

    目录 题目 思路 \(Code\) 题目 P3884 [JLOI2009]二叉树问题 思路 深搜统计深度,倍增\(\text{LCA}\)求边数 \(Code\) #include<iostre ...

  5. 产品生命周期(Product Life Circle,PLC)

    什么是产品生命周期? 产品生命周期是新产品从开发进入市场到被市场淘汰的整个过程.产品生命周期可分为初创期.成长期.成熟期.衰退期. 产品生命周期有什么用? 在产品不同的生命阶段,公司的业务目的都不同. ...

  6. Uncaught ReferenceError: Invalid left-hand side in assignment

    Uncaught ReferenceError: Invalid left-hand side in assignment 今天在对个人资料页面增加当浏览别的页面之后第二次访问当前页面,之前填写的内容 ...

  7. Linux crontab命令:循环执行定时任务(详解版)

    前面学习了 at 命令,此命令在指定的时间仅能执行一次任务,但在实际工作中,系统的定时任务一般是需要重复执行的.而 at 命令显然无法满足需求,这是就需要使用 crontab 命令来执行循环定时任务. ...

  8. Servlet相关的几种乱码

    1. 页面中文显示乱码 原因: response中的内容会先输入到response缓冲区,然后再输入到传给浏览器,所以要将缓冲区和浏览器的编码都设置成utf-8 1)未使用jsp,而是在servlet ...

  9. tomcat请求响应代码分享

    NioEndpoint的Poller轮询器持续进行扫描是否有新的event()方法调用后产生新的event配置. 发现后执行AbstractProtocol.class中的process()方法进行处 ...

  10. Android 动态更换桌面图标

    每当双 11.12 来临之际,Android 手机 Launcher 中的淘宝.天猫图标就会变成双 11.12 主题的图标.实现了动态切换图标.名称 MainActivity package com. ...