python最多接受2个参数

时间:2019-05-03 13:57:22

标签: python python-3.x

我正在研究简单的strategy模式实现。我来自Java世界,因此我将所有类划分为单独的文件,因为这对我来说似乎不错。但是我在打包和导入时遇到了麻烦,请参见下面的代码以获取详细信息。

所以我有一只鸭子,有两种封装的行为:苍蝇和嘎嘎。

总体结构为:

behavior/
├── __init__.py
├── __init__.pyc
├── __pycache__
│   └── __init__.cpython-37.pyc
├── fly
│   ├── FlyBehavior.py
│   ├── FlyBehavior.pyc
│   ├── FlyNoWay.py
│   ├── FlyRocketPowered.py
│   ├── FlyRocketPowered.pyc
│   ├── FlyWithWings.py
│   ├── __init__.py
│   ├── __init__.pyc
│   └── __pycache__
│       ├── FlyBehavior.cpython-37.pyc
│       ├── FlyRocketPowered.cpython-37.pyc
│       └── __init__.cpython-37.pyc
└── quack
    ├── MuteQuack.py
    ├── Quack.py
    ├── QuackBehavior.py
    ├── Squeak.py
    └── __init__.py
duck
├── Duck.py
├── MallardDuck.py
├── ModelDuck.py
├── __init__.py
├── __init__.pyc
└── __pycache__
    ├── Duck.cpython-37.pyc
    ├── MallardDuck.cpython-37.pyc
    └── __init__.cpython-37.pyc
MiniDuckSimulator.py
MiniDuckSimulator.pyc

飞行舱:

FlyBehavior.py

from abc import ABC, abstractmethod

class FlyBehavior(ABC):

    @abstractmethod
    def fly(self):
        pass

FlyNoWay.py

from behavior.fly import FlyBehavior

class FlyNoWay(FlyBehavior):

    def fly(self):
        print("I can't fly")

FlyRocketPowered.py

from behavior.fly import FlyBehavior

class FlyRocketPowered(FlyBehavior):

    def fly(self):
        print("I'm flying with a rocket")

FlyWithWings.py

from behavior.fly import FlyBehavior

class FlyWithWings(FlyBehavior):

    def fly(self):
        print("I'm flying!")

庸俗类:

QuackBehavior.py

from abc import ABC, abstractmethod

class QuackBehavior(ABC):

    @abstractmethod
    def quack(self):
        pass

Quack.py

from behavior.quack import QuackBehavior

class Quack(QuackBehavior):

    def quack(self):
        print('Quack')

MuteQuack.py

from behavior.quack import QuackBehavior

class MuteQuack(QuackBehavior):

    def quack(self):
        print('<< Silence >>')

Squeak.py

from behavior.quack import QuackBehavior

class Squeak(QuackBehavior):

    def quack(self):
        print('Squeak')

鸭类:

Duck.py

from abc import ABC, abstractmethod

class Duck(ABC):

    fly_behavior = None
    quack_behavior = None

    @abstractmethod
    def display(self):
        pass

    def perform_fly(self):
        self.fly_behavior.fly()

    def perform_quack(self):
        self.quack_behavior.quack()

    def swim(self):
        print('All ducks float, even decoys!')

MallardDuck.py

from duck import Duck

class MallardDuck(Duck):

    def __init__(self):
        self.quack_behavior = Quack()
        self.fly_behavior = FlyWithWings()

    def display(self):
        print("I'm a real Mallard duck")

ModelDuck.py

from duck import Duck

class ModelDuck(Duck):

    def __init__(self):
        self.fly_behavior = FlyNoWay()
        self.quack_behavior = Quack()

    def display(self):
        print("I'm a model duck")

主要类别:

MiniDuckSimulator.py

from duck import MallardDuck
from duck import ModelDuck
import behavior.fly.FlyRocketPowered as FlyRocketPowered

if __name__ == "__main__":
    mallard = MallardDuck()
    mallard.display()
    mallard.perform_fly()
    mallard.perform_quack()

    model = ModelDuck()
    model.display()
    model.perform_fly()
    model.fly_behavior = FlyRocketPowered()
    model.perform_fly()

程序输出:

$ python MiniDuckSimulator.py 
Traceback (most recent call last):
  File "MiniDuckSimulator.py", line 1, in <module>
    from duck import MallardDuck
  File "/Users/p/Documents/Develop/patterns_book/00_strategy/py/duck/MallardDuck.py", line 3, in <module>
    class MallardDuck(Duck):
TypeError: module() takes at most 2 arguments (3 given)

我的模块怎么了?我存储和构造代码/类的方法可以吗?谢谢。

1 个答案:

答案 0 :(得分:4)

应该是

from duck.MallardDuck import MallardDuck

您要导入的MallardDuck仍然是文件,而不是类,即使其中只有一个类也是如此。

以类似的方式更改所有其他导入。

一些注意事项:

  • 策略模式在python中是无用的,您可以直接将函数作为参数传递,而不是将其包装在类中
  • 没有理由每个文件只有一个班级,可以根据您的判断使用文件将相关班级分组
  • 按照惯例,模块(文件)名称均为小写,类名称仅为驼峰式