class Dog:count = 0 # this is a class variabledogs = [] # this is a class variable
def __init__(self, name):self.name = name #self.name is an instance variableDog.count += 1Dog.dogs.append(name)
def bark(self, n): # this is an instance methodprint("{} says: {}".format(self.name, "woof! " * n))
def rollCall(n): #this is implicitly a class method (see comments below)print("There are {} dogs.".format(Dog.count))if n >= len(Dog.dogs) or n < 0:print("They are:")for dog in Dog.dogs:print(" {}".format(dog))else:print("The dog indexed at {} is {}.".format(n, Dog.dogs[n]))
fido = Dog("Fido")fido.bark(3)Dog.rollCall(-1)rex = Dog("Rex")Dog.rollCall(0)
class Dog:count = 0 # this is a class variabledogs = [] # this is a class variable
def __init__(self, name):self.name = name #self.name is an instance variableDog.count += 1Dog.dogs.append(name)
def bark(self, n): # this is an instance methodprint("{} says: {}".format(self.name, "woof! " * n))
@staticmethoddef rollCall(n):print("There are {} dogs.".format(Dog.count))if n >= len(Dog.dogs) or n < 0:print("They are:")for dog in Dog.dogs:print(" {}".format(dog))else:print("The dog indexed at {} is {}.".format(n, Dog.dogs[n]))
fido = Dog("Fido")fido.bark(3)Dog.rollCall(-1)rex = Dog("Rex")Dog.rollCall(0)rex.rollCall(-1)
# garden.pydef trim(a):pass
def strip(a):pass
def bunch(a, b):pass
def _foo(foo):pass
class powertools(object):"""Provides much regarded gardening power tools."""@staticmethoddef answer_to_the_ultimate_question_of_life_the_universe_and_everything():return 42
@staticmethoddef random():return 13
@staticmethoddef promise():return True
def _bar(baz, quux):pass
class _Dice(object):pass
class _6d(_Dice):pass
class _12d(_Dice):pass
class _Smarter:pass
class _MagicalPonies:pass
class _Samurai:pass
class Foo(_6d, _Samurai):pass
class Bar(_12d, _Smarter, _MagicalPonies):pass
…
# tests.pyimport unittestimport garden
class GardenTests(unittest.TestCase):pass
class PowertoolsTests(unittest.TestCase):pass
class FooTests(unittest.TestCase):pass
class BarTests(unittest.TestCase):pass
# my_garden.pyimport gardenfrom garden import powertools
class _Cowboy(garden._Samurai):def hit():return powertools.promise() and powertools.random() or 0
class Foo(_Cowboy, garden.Foo):pass
class Calculator:@staticmethoddef multiply(n1, n2, *args):Res = 1for num in args: Res *= numreturn n1 * n2 * Res
print(Calculator.multiply(1, 2, 3, 4)) # 24