【问题标题】:Class in Python [closed]Python中的类[关闭]
【发布时间】:2013-12-28 13:57:26
【问题描述】:

我正在尝试用几种不同的方法创建一个关于鸟类的类。这只是理论上的代码。从句法或语义的角度来看,代码有什么本质上的错误吗?如果是这样,请修复。

class Bird(object):
    def __init__(self, Height, Weight, HasFeathers, CapableofFlying, BirdMigrates, BirdSings, BirdEatsworms):
        self.Height=Height
        self.Weight=Weight
        self.HasFeathers=HasFeathers
        self.HasFeathers= HasFeathers
        self.CapableofFlying= getCapableofFlying
        self.BirdMigrates=BirdMigrates
        self.BirdSings=BirdSings
        self.BirdEatsworms=BirdEatsworms

def Height():
    return Height

def Weight():
    return Weight

def HasFeathers():
    if  getHasFeathers == "No":
        return “The bird does not have feathers”
    else:
        return “The bird has feathers”

def CapableofFlying():
    if CapableofFlying== "No":
        return “The bird is incapable of flying”
    else:
        return “The bird  is capable of flying”

def BirdMigrates():
    if BirdMigrates= "No":
        return “The bird does not migrate”
    else:
        return “The bird migrates”

def BirdSings():
    if BirdSings= "No":
        return “The bird cannot sing”
    else:
        return “The bird  can sing”

def BirdEatsworms():
    if BirdEatsworms= "No":
        return “The bird does not eat worms”
    else:
        return “The bird  eats worms”

【问题讨论】:

  • 以“:”结尾的每一行,下一行(以及该块中您想要的所有行)都应该从带有“:”的行缩进一级
  • 嗯,缩进全错了,一方面,然后还有很多其他的东西。也许你应该先看看 Python 说了什么?
  • 这很难说,因为缩进很远。你有机会解决这个问题吗?
  • 有几个语法错误...例如不是if x=0:而是if x==0,请修复缩进,因为无法知道您是否想要这些功能在内部或外部类(在这两种情况下我都看到错误)。
  • __init__ 中,您是否将类方法定义为您在类之外编写的函数?请修正缩进。

标签: python class


【解决方案1】:
  1. pip install pylint
  2. pylint <my_file_that_contains_Bird_class.py>

试试这个,它会向您展示格式化代码所需的几乎所有内容。

如果您想更深入一点,请执行pip install pep8 并运行pep8 <my_file>.py

【讨论】:

    【解决方案2】:
    class Bird(object):
        def __init__(self, height, weight, has_feathers, capable_of_flying, bird_migrates, bird_sings, bird_eats_worms):
            self.height = height
            self.weight = weight
            self.has_feathers = has_feathers
            self.capable_of_flying = capable_of_flying
            self.bird_migrates = bird_migrates
            self.bird_sings = bird_sings
            self.bird_eats_worms = bird_eats_worms
    
        def get_height(self):
            return self.height
    
        def get_weight(self):
            return self.weight
    
        def get_has_feathers(self):
            if self.has_feathers:
                return "The bird has feathers"
            else:
                return "The bird does not have feathers"
    
        # Etc...
    

    注意几点:

    • 你不能拥有同名的函数和变量,所以我不得不将函数重命名为“get_...”
    • 如果你想让一个函数成为类的一部分(称为方法),那么它必须缩进到类中。
    • 类方法需要将类对象作为第一个参数传递给它(按约定称为self)。这就是类了解自身信息的方式。
    • 类中的变量需要用classvar.variable访问,其中classvar类内部是self
    • CamelCase 只能用于 python 中的类名。对变量和函数名使用 all_lower_case_and_underscore。

    你会像这样使用这个类:

    penguin = Bird(10, 30, True, False, False, True, False)
    print penguin.get_has_feathers() # "The bird has feathers"
    

    整个“get_...”范式是 python 中的“反模式”(您可能有 C++ 背景,这被认为是正确的做法)。最好直接暴露变量:

    class Bird(object):
        def __init__(self, height, weight, has_feathers, capable_of_flying, bird_migrates, bird_sings, bird_eats_worms):
            self.height = height
            self.weight = weight
            if has_feathers:
                self.has_feathers = "The bird has feathers"
            else:
                self.has_feathers = "The bird does not have feathers"
            # etc...
    
    penguin = Bird(10, 30, True, False, False, True, False)
    print penguin.has_feathers # "The bird has feathers"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-09
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2020-10-24
      • 2013-08-04
      • 2021-11-01
      相关资源
      最近更新 更多