liv2hak
liv2hak

Reputation: 15010

Object instantiation in Python

I have the following python code for the class PortDataState given below.

class PortDataState(dict):
    # dict: Port class -> PortData class
    # slimed down version of OrderedDict as python 2.6 doesn't support it.
    _PREV = 0
    _NEXT = 1
    _KEY = 2

    def __init__(self):
        super(PortDataState, self).__init__()
        self._root = root = []          # sentinel node
        root[:] = [root, root, None]    # [_PREV, _NEXT, _KEY]
                                        # doubly linked list
        self._map = {}

    def _remove_key(self, key):
        link_prev, link_next, key = self._map.pop(key)
        link_prev[self._NEXT] = link_next
        link_next[self._PREV] = link_prev

    def _append_key(self, key):
        root = self._root
        last = root[self._PREV]
        last[self._NEXT] = root[self._PREV] = self._map[key] = [last, root,
                                                                key]

    def _prepend_key(self, key):
        root = self._root
        first = root[self._NEXT]
        first[self._PREV] = root[self._NEXT] = self._map[key] = [root, first,
                                                                 key]

    def _move_last_key(self, key):
        self._remove_key(key)
        self._append_key(key)

    def _move_front_key(self, key):
        self._remove_key(key)
        self._prepend_key(key)

    def add_port(self, port, lldp_data):
        if port not in self:
            self._prepend_key(port)
            self[port] = PortData(port.is_down(), lldp_data)
        else:
            self[port].is_down = port.is_down()

    def lldp_sent(self, port):
        port_data = self[port]
        port_data.lldp_sent()
        self._move_last_key(port)
        return port_data

    def lldp_received(self, port):
        self[port].lldp_received()

    def move_front(self, port):
        port_data = self.get(port, None)
        if port_data is not None:
            port_data.clear_timestamp()
            self._move_front_key(port)

    def set_down(self, port):
        is_down = port.is_down()
        port_data = self[port]
        port_data.set_down(is_down)
        port_data.clear_timestamp()
        if not is_down:
            self._move_front_key(port)
        return is_down

    def get_port(self, port):
        return self[port]

    def del_port(self, port):
        del self[port]
        self._remove_key(port)

    def __iter__(self):
        root = self._root
        curr = root[self._NEXT]
        while curr is not root:
            yield curr[self._KEY]
            curr = curr[self._NEXT]

    def clear(self):
        for node in self._map.itervalues():
            del node[:]
        root = self._root
        root[:] = [root, root, None]
        self._map.clear()
        dict.clear(self)

    def items(self):
        'od.items() -> list of (key, value) pairs in od'
        return [(key, self[key]) for key in self]

    def iteritems(self):
        'od.iteritems -> an iterator over the (key, value) pairs in od'
        for k in self:
            yield (k, self[k])

In another class I am instantiating an object of PortDataState as below

self.ports = PortDataState()  # Port class -> PortData class

I am very new to Python.I want to know what does self.ports contain? I don't really get what __init__ function of PortDataState is doing above.Specifically what does

def __init__(self):
    super(PortDataState, self).__init__()
    self._root = root = []          # sentinel node
    root[:] = [root, root, None]    # [_PREV, _NEXT, _KEY]
                                    # doubly linked list
    self._map = {}

mean?

Upvotes: 0

Views: 2448

Answers (3)

mblw
mblw

Reputation: 1798

__init__ function in Python is constructor method as any other language. This method initialize class fields (but __init__ method is not to create object itself, __init__ only modified object after it has been created). Object creation performed automatically when your write PortDataState() (i.e. when object instantiating).

As for your case object self.ports will contain all methods and variables that dict class contained (because PortDataStates inherits from dict). Besides your object will contain:

  1. _PREV, _NEXT, _KEY variables (it a static variables and will be shared between each of objects of PortDataState class)
  2. _root, _map - some kind of "protected" members (if your use IDE, e.g. PyCharm, it warn you that this members protected if you try to access to one of these member outside class). But really during execution Python not prevent you to access on member starts with "_" char. (So it's only convention).
  3. link_prev, first, etc. will not be presented in object because its not started with keyword self. self is object itself.

Upvotes: 4

Dmitry Zagorulkin
Dmitry Zagorulkin

Reputation: 8548

I want to know what does self.ports contain?

Python have very usefull dir() function. If you want get the object content write this:

print dir(self.ports)

Upvotes: 1

glglgl
glglgl

Reputation: 91149

__init__ initializes the user-defined class so that it can be used as intended.

In this example,

  • the __init__ of the super class is invoked
  • the _root and _map attributes of the object to be initialized (which is self) are set so that they can be used later in other methods.

The function is called upon instantiation of a new object (instance) of this class.

What happens with

self.ports = PortDataState()  # Port class -> PortData class

is that a new object of class PortDataState is instantiated, so the __init__ method is called.

Upvotes: 2

Related Questions