高手,如何用python获取python 变量内存地址址

您还可以使用以下方式登录
当前位置:&>&&>&&>& > python中查看变量内存地址的方法
python中查看变量内存地址的方法
&  这篇文章主要介绍了python中查看变量内存地址的方法,涉及Python中id使用技巧,需要的朋友可以参考下  本文实例讲述了python中查看变量内存地址的方法。分享给大家供大家参考。具体实现方法如下:  这里可以使用id  ?
&&& print id.__doc__
id(object) -& integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
  希望本文所述对大家的Python程序设计有所帮助。
欢迎转载:
相关推荐:保持成长,保持好奇,保持安静,保持沉默。
python中查看变量内存地址的方法
转自:http://www.jb51.net/article/65475.htm
本文实例讲述了python中查看变量内存地址的方法。分享给大家供大家参考。具体实现方法如下:
这里可以使用id
id(object) -& integer
Return the identity of an object.
This is guaranteed to be unique among
simultaneously existing objects.
(Hint: it's the object's memory address.)
id(x)得到x变量的内存地址(10进制)
没有更多推荐了,可以使用id
&&& print id.__doc__
id(object) -& integer
Return the identity of an object.
This is guaranteed to be unique among
simultaneously existing objects.
(Hint: it's the object's memory address.)
阅读(...) 评论()其他回答(1)
那个不是内存地址只是唯一识别码,可以看做是一个自增长的整数,跟内存地址无关
一切皆对象!
id出来的数字跟py对象的执行没有关系,只是标识符而已。
园豆:3262
园豆:3262
清除回答草稿
&&&您需要以后才能回答,未注册用户请先。&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子
摘要:例子一:Python用WMI模块获取windowns系统的硬件信息:硬盘分区、使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息。#!/usr/bin/envpython#-*-coding:utf-8-*-importwmiimportosimportsysimportplatformimporttimedefsys_version():&&&&c=wmi.WMI()&&&
Python用WMI模块获取windowns系统的硬件信息:硬盘分区、使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wmi
import sys
import platform
import time
def sys_version():&
&&& c = wmi.WMI ()
&&& #获取操作系统版本
&&& for sys in c.Win32_OperatingSystem():
&&&&&&& print &Version:%s& % sys.Caption.encode(&UTF8&),&Vernum:%s& % sys.BuildNumber
&&&&&&& print& sys.OSArchitecture.encode(&UTF8&)#系统是32位还是64位的
&&&&&&& print sys.NumberOfProcesses #当前系统运行的进程总数
def cpu_mem():
&&& c = wmi.WMI ()&&&&&&&
&&& #CPU类型和内存
&&& for processor in c.Win32_Processor():
&&&&&&& #print &Processor ID: %s& % processor.DeviceID
&&&&&&& print &Process Name: %s& % processor.Name.strip()
&&& for Memory in c.Win32_PhysicalMemory():
&&&&&&& print &Memory Capacity: %.fMB& %(int(Memory.Capacity)/1048576)
def cpu_use():
&&& #5s取一次CPU的使用率
&&& c = wmi.WMI()
&&& while True:
&&&&&&& for cpu in c.Win32_Processor():
&&&&&&&&&&&& timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
&&&&&&&&&&&& print '%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage)
&&&&&&&&&&&& time.sleep(5)&&&&
def disk():
&&& c = wmi.WMI ()&&&
&&& #获取硬盘分区
&&& for physical_disk in c.Win32_DiskDrive ():
&&&&&&& for partition in physical_disk.associators (&Win32_DiskDriveToDiskPartition&):
&&&&&&&&&&& for logical_disk in partition.associators (&Win32_LogicalDiskToPartition&):
&&&&&&&&&&&&&&& print physical_disk.Caption.encode(&UTF8&), partition.Caption.encode(&UTF8&), logical_disk.Caption
&&& #获取硬盘使用百分情况
&&& for disk in c.Win32_LogicalDisk (DriveType=3):
&&&&&&& print disk.Caption, &%0.2f%% free& % (100.0 * long (disk.FreeSpace) / long (disk.Size))
def network():
&&& c = wmi.WMI ()&&&&
&&& #获取MAC和IP地址
&&& for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
&&&&&&& print &MAC: %s& % interface.MACAddress
&&& for ip_address in interface.IPAddress:
&&&&&&& print &ip_add: %s& % ip_address
&&& #获取自启动程序的位置
&&& for s in c.Win32_StartupCommand ():
&&&&&&& print &[%s] %s &%s&& % (s.Location.encode(&UTF8&), s.Caption.encode(&UTF8&), s.Command.encode(&UTF8&))&
&&& #获取当前运行的进程
&&& for process in c.Win32_Process ():
&&&&&&& print process.ProcessId, process.Name
def main():
&&& sys_version()
&&& #cpu_mem()
&&& #disk()
&&& #network()
&&& #cpu_use()
if __name__ == '__main__':
&&& main()
&&& print platform.system()
&&& print platform.release()
&&& print platform.version()
&&& print platform.platform()
&&& print platform.machine()
由于我用到的不多,所以只获取的CPU、内存和硬盘,如果需要其它资源,请参照msdn。
import os import win32api import win32con import wmi import time
def getSysInfo(wmiService = None): &&& result = {} &&& if wmiService == None: &&&&&&& wmiService = wmi.WMI() &&& # cpu &&& for cpu in wmiService.Win32_Processor(): &&&&&&& timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()) &&&&&&& result['cpuPercent'] = cpu.loadPercentage &&& # memory &&& cs = wmiService.Win32_ComputerSystem() &&& os = wmiService.Win32_OperatingSystem() &&& result['memTotal'] = int(int(cs[0].TotalPhysicalMemory)/) &&& result['memFree'] = int(int(os[0].FreePhysicalMemory)/1024) &&& #disk &&& result['diskTotal'] = 0 &&& result['diskFree'] = 0 &&& for disk in wmiService.Win32_LogicalDisk(DriveType=3): &&&&&&& result['diskTotal'] += int(disk.Size) &&&&&&& result['diskFree'] += int(disk.FreeSpace) &&& result['diskTotal'] = int(result['diskTotal']/) &&& result['diskFree'] = int(result['diskFree']/) &&& return result
if __name__ == '__main__': &&& wmiService = wmi.WMI() &&& while True: &&&&&&& print getSysInfo(wmiService) &&&&&&& time.sleep(3)
采用的wmi模块获取的,由于wmi初始化时占用系统资源太高,所以如果需要循环获取,请在循环体外面把wmi对象初始化好,然后传入函数里面,这样就不会产生CPU资源过高的情况。
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子相关信息,包括
的信息,所有使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International

我要回帖

更多关于 python 获取内存地址 的文章

 

随机推荐