[Python] Understand Union__python

Source: Internet
Author: User

Don't understand the data structure bitter AH

The Union occupies only one memory space in memory, the size of which is determined by the largest data type in the Union, and when the Union is initialized, the value of the Union is determined by the last valid parameter

' Created on
2012-9-9

@author: Jincheng ' '
ctypes import *

print "AAA:"
value = Raw_ Input ()
V=int (value)
Vv=long (value)
Vvv=value
Class aaa (Union):
    _fields_=[
       ("AAA", c_ int),
       ("BBB", C_long),
       ("CCC", C_char),       ]

print "aaaaaaa:%s"%value

a=aaa (V,VV,VVV)

Print "AAA:%d"%a.aaa
print "BBB:%ld"%a.bbb
print "CCC:%s"%A.CCC
Test1

C:\python27>python D:\jincheng\workspace\GrayHatPython\chapter1.py
AAA:
6
aaaaaaa:6
AAA:
bbb:54
Ccc:6
Test2

C:\python27>python D:\jincheng\workspace\GrayHatPython\chapter1.py
AAA:
aaaaaaa:66

aaa:13878
bbb:13878
ccc:66
Modify

From ctypes import *

print "AAA:"
value = Raw_input ()
v=int (value)

print "v%d"%v

print "BBB:"
Val=raw_input ()
Vv=long (val)
Vvv=value
Class aaa (Union):
    _fields_=[
       ("AAA", C_int),
       (" BBB ", C_long),
       (" CCC ", C_char * 6),       ]

print" aaaaaaa:%s "%value

a=aaa (V,VV)

s=a.aaa

SS =int (s)

print "ss%d"%ss

print "AAA:%d"%a.aaa
print "BBB:%ld"%a.bbb
print "CCC:%s"%A.CCC
Test
D:\python27>python d:\demo.py
AAA:
v
-BBB:
aaaaaaa:66
SS
aaa:55
bbb:55
ccc:7


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.