= @staticmethod und @classmethod =

Die slides befinden sich im Anhang dieser Seite. Zur "schöneren" Darstellung muss der String $REVEALJS_ROOT im file auf eine valide [[https://github.com/hakimel/reveal.js/|reveal.js]] location geändert werden - reveal.js ist jedenfalls mal einen Blick wert!

{{{#!highlight python


class A(object):
    def foo(self, x):
        print("executing foo({}, {})".format(self, x))

    @classmethod
    def class_foo(cls, x):
        print("executing class_foo({}, {})".format(cls, x))

    @staticmethod
    def static_foo(x):
        print("executing static_foo({})".format(x))

a = A()

a.foo(1)

a.foo

A.class_foo(1)

A.class_foo

a.class_foo(1)

a.static_foo(1)

A.static_foo('hi')


class Date(object):
    def __init__(self, year=0, month=0, day=0):
        self.day = day
        self.month = month
        self.year = year

    def __repr__(self):
        return "It's the {}.{}.{}".format(self.day, self.month, self.year)

Date(2014, 4, 1)

string_date = '01-04-2014'
day, month, year = map(int, string_date.split('-'))
date1 = Date(year, month, day)
date1


class Date(object):
    def __init__(self, year=0, month=0, day=0):
        self.day = day
        self.month = month
        self.year = year

    def __repr__(self):
        return "It's the {}.{}.{}".format(self.day, self.month, self.year)

    @classmethod
    def from_string(cls, date_as_string):
        day, month, year = map(int, date_as_string.split('-'))
        date1 = cls(year, month, day)
        return date1

Date.from_string('01-04-2014')

date_as_string = '01-04-2014'
day, month, year = map(int, date_as_string.split('-'))
day <= 31 and month <= 12 and year <= 3999


class Date(object):
    def __init__(self, year=0, month=0, day=0):
        self.day = day
        self.month = month
        self.year = year

    def __repr__(self):
        return "It's the {}.{}.{}".format(self.day, self.month, self.year)

    @staticmethod
    def is_date_valid(date_as_string):
        day, month, year = map(int, date_as_string.split('-'))
        return day <= 31 and month <= 12 and year <= 3999

Date.is_date_valid('01-04-2014')


}}}