在 Python 中编写 DRY 代码
Writing DRY Code in Python
我真的是开发新手,我正在将 Codecademy 的 Battleship 游戏重写为 2 个玩家。我真的很挣扎,但到目前为止,我觉得这是一次很好的锻炼。我想保持良好的 OOP 和 DRY 原则。不过我有个问题。我试图通过用户输入来创建对象,并且我必须创建两个相似的定义来实现我的目标。我只写了一部分程序,我一直在尽可能地测试。要了解我的问题,请查看下面的 get_name1 和 get_name2:
from random import randint
class Person(object):
def __init__(self, name, turn):
self.name = name
self.turn = turn
def get_name1():
while 1:
name = input("What is the name of Player 1? ")
if name.isalpha() == False:
print("\nPlease share your name with me.\n")
else:
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return name
break
def get_name2():
while 1:
name = input("What is the name of Player 2? ")
if name.isalpha() == False:
print("\nPlease share your name with me.\n")
else:
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return name
break
Player1 = Person(Person.get_name1(), 1)
Player2 = Person(Person.get_name2(), 2)
print("Welcome to Battleship, %s!" % Player1.name)
print("You will take turn %s.\n" % Player1.turn)
print("Welcome to Battleship, %s!" % Player2.name)
print("You will take turn %s.\n" % Player2.turn)
有没有一种方法可以将 get_name1 和 get_name2 合并到一个函数中,同时保留 "What is the name of Player 1?" 和 "What is the name of Player 2?" 的唯一输入行并仍然将唯一输入传递给两个不同的 class 对象?
def get_name(name_str):
while 1:
name = input("What is the name of %s? " % name_str)
if name.isalpha() == False:
print("\nPlease share your name with me.\n")
else:
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return name
break
Player1 = Person(Person.get_name('Player 1'), 1)
Player2 = Person(Person.get_name('Player 2'), 2)
所以这两个函数唯一的区别就是播放器编号?然后将玩家编号作为参数传递给函数,并使用字符串格式进行提示。
def get_name(player_number):
prompt = 'What is the name of Player {}? '.format(player_number)
while True:
name = input(prompt)
# rest of code goes here..
通常,当您发现两个几乎相同的函数时,您会寻找不同之处,将其设为变量,然后将该变量作为参数传递。那么你只有一个函数,它被它的参数修改了。
最好,用一个classmethod一次性创建一个人名字:
class Person(object):
def __init__(self, name, turn):
self.name = name
self.turn = turn
@classmethod
def create(cls, turn):
while True:
name = input("What is the name of Player %d? " % turn)
if name.isalpha():
break;
print("\nPlease share your name with me.\n")
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return cls(name, turn)
Player1 = Person.create(1)
Player2 = Person.create(2)
print("Welcome to Battleship, %s!" % Player1.name)
print("You will take turn %s.\n" % Player1.turn)
我真的是开发新手,我正在将 Codecademy 的 Battleship 游戏重写为 2 个玩家。我真的很挣扎,但到目前为止,我觉得这是一次很好的锻炼。我想保持良好的 OOP 和 DRY 原则。不过我有个问题。我试图通过用户输入来创建对象,并且我必须创建两个相似的定义来实现我的目标。我只写了一部分程序,我一直在尽可能地测试。要了解我的问题,请查看下面的 get_name1 和 get_name2:
from random import randint
class Person(object):
def __init__(self, name, turn):
self.name = name
self.turn = turn
def get_name1():
while 1:
name = input("What is the name of Player 1? ")
if name.isalpha() == False:
print("\nPlease share your name with me.\n")
else:
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return name
break
def get_name2():
while 1:
name = input("What is the name of Player 2? ")
if name.isalpha() == False:
print("\nPlease share your name with me.\n")
else:
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return name
break
Player1 = Person(Person.get_name1(), 1)
Player2 = Person(Person.get_name2(), 2)
print("Welcome to Battleship, %s!" % Player1.name)
print("You will take turn %s.\n" % Player1.turn)
print("Welcome to Battleship, %s!" % Player2.name)
print("You will take turn %s.\n" % Player2.turn)
有没有一种方法可以将 get_name1 和 get_name2 合并到一个函数中,同时保留 "What is the name of Player 1?" 和 "What is the name of Player 2?" 的唯一输入行并仍然将唯一输入传递给两个不同的 class 对象?
def get_name(name_str):
while 1:
name = input("What is the name of %s? " % name_str)
if name.isalpha() == False:
print("\nPlease share your name with me.\n")
else:
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return name
break
Player1 = Person(Person.get_name('Player 1'), 1)
Player2 = Person(Person.get_name('Player 2'), 2)
所以这两个函数唯一的区别就是播放器编号?然后将玩家编号作为参数传递给函数,并使用字符串格式进行提示。
def get_name(player_number):
prompt = 'What is the name of Player {}? '.format(player_number)
while True:
name = input(prompt)
# rest of code goes here..
通常,当您发现两个几乎相同的函数时,您会寻找不同之处,将其设为变量,然后将该变量作为参数传递。那么你只有一个函数,它被它的参数修改了。
最好,用一个classmethod一次性创建一个人名字:
class Person(object):
def __init__(self, name, turn):
self.name = name
self.turn = turn
@classmethod
def create(cls, turn):
while True:
name = input("What is the name of Player %d? " % turn)
if name.isalpha():
break;
print("\nPlease share your name with me.\n")
print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
return cls(name, turn)
Player1 = Person.create(1)
Player2 = Person.create(2)
print("Welcome to Battleship, %s!" % Player1.name)
print("You will take turn %s.\n" % Player1.turn)