相关疑难解决方法(0)

使用__init __()方法理解Python super()

我正在努力了解它的用法super().从它的外观来看,可以创建两个子类,就好了.

我很想知道以下2个孩子班级之间的实际差异.

class Base(object):
    def __init__(self):
        print "Base created"

class ChildA(Base):
    def __init__(self):
        Base.__init__(self)

class ChildB(Base):
    def __init__(self):
        super(ChildB, self).__init__()

ChildA() 
ChildB()
Run Code Online (Sandbox Code Playgroud)

python oop inheritance class super

2366
推荐指数
7
解决办法
158万
查看次数

设置 ctypes.Structure 默认值

这不起作用:

class ifinfomsg(ctypes.Structure):
    _fields_ = [
                 ('ifi_family',  ctypes.c_ubyte),
                 ('__ifi_pad',   ctypes.c_ubyte),
                 ('ifi_type',    ctypes.c_ushort),
                 ('ifi_index',   ctypes.c_int),
                 ('ifi_flags',   ctypes.c_uint),
                 ('ifi_change',  ctypes.c_uint(0xFFFFFFFF))
               ]
Run Code Online (Sandbox Code Playgroud)

它的错误是:

  File "rtnetlink.py", line 243, in <module>
    class ifinfomsg(ctypes.Structure):
TypeError: Error when calling the metaclass bases
    second item in _fields_ tuple (index 5) must be a C type
Run Code Online (Sandbox Code Playgroud)

但是我可以设置以下值__init__()

class ifinfomsg(ctypes.Structure):
    _fields_ = [
                 ('ifi_family',  ctypes.c_ubyte),
                 ('__ifi_pad',   ctypes.c_ubyte),
                 ('ifi_type',    ctypes.c_ushort),
                 ('ifi_index',   ctypes.c_int),
                 ('ifi_flags',   ctypes.c_uint),
                 ('ifi_change',  ctypes.c_uint)
               ]

    def __init__(self):
        self.__ifi_pad = 0
        self.ifi_change = 0xFFFFFFFF
Run Code Online (Sandbox Code Playgroud)

这是通过 执行此操作的“正确”方法吗__init__

python ctypes

5
推荐指数
1
解决办法
2404
查看次数

标签 统计

python ×2

class ×1

ctypes ×1

inheritance ×1

oop ×1

super ×1