/ Python

Python Syntax

Default value

Assign default value.

blabla = bla or u'blabla'

Not to be used as class or method default value for parameters, since one can directly do

def __init__(self,
             name="Horse with no name",
             alias="Dat horse",

Object special method

How to specify the behavior for str(myobject) or myobject1 + myobject2. This is defined amongst other places in https://www.python.org/dev/peps/pep-0207/, https://www.python.org/dev/peps/pep-0335/ etc.

def __str__(self):
        return self.name

    def __repr__(self):
        return ','.join(["%s = %r" % (k, v) for k, v in self.__dict__.iteritems()])

    def __eq__(self, other):
        macrodiff = DiffDict(self.macros, other.macros)

        if self.name == other.name and \
           self.alias == other.alias and \
           self.ipaddr == other.ipaddr and \           
           sorted(self.services, key=lambda x: x.name) == sorted(other.services, key=lambda x: x.name) and \
           self.actionurl == other.actionurl and \
           self.templates == other.templates and \
           not macrodiff.added() and \
           not macrodiff.changed():
           return True
        return False

    def __ne__(self, other):
        return not self.__eq__(other)
Python Syntax
Share this