小编noo*_*014的帖子

Python类继承没有做我认为应该做的事情

我想我理解了python中的类继承,基本上你可以继承子类的父类的属性以便重用,并且还可以"添加"它以使更复杂的类.

这是我的问题:我有一个Car下面的类有(model, color, mpg)参数,之后我创建了一个名为ElectricCar从父Car类继承的新子类...现在当我调用ElectricCar"时(battery_type, model, color, mpg),我得到以下错误:

TypeError:init()只需要2个参数(给定5个参数)

我知道要解决它.我需要添加self.model,self.colorself.mpgElectricCar类.但为什么我必须这样做?如果我需要在子类上重新定义,这似乎会破坏继承的目的.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg

my_car = Car("DeLorean", "silver", 88)

class ElectricCar(Car):
    def __init__(self,battery_type):
        self.battery_type = battery_type

my_car = ElectricCar("molten salt", "Honda","black", "33")
Run Code Online (Sandbox Code Playgroud)

python python-2.7

2
推荐指数
1
解决办法
71
查看次数

如何在向下滚动网页时使图片发生变化?

我在这个页面上发现了这个非常酷的效果,向下滚动 2/3 即可看到它。这是一个“铅笔”图像(来自不同角度的多个图像),当您向下滚动时,它会发生变化,如果您向上滚动,它也会做同样的事情。链接在这里https://www.fiftythird.com/pencil

不管怎样,谁能告诉我如何在我的网站上实现这一点?或者效果的名称,以便我可以查找。

非常感激。

html javascript css jquery

2
推荐指数
1
解决办法
1600
查看次数

Python循环遍历列表但没有找到值

谢谢你提前看看这个!基本上我有一个while循环是循环在试图找到数字1我的代码的下方,它遍历"几乎"所有的列表中,但没有找到索引值1,任何见解列表?

numbers = [24, 10, 92, 28, 71, 1, 80, 70]
counter = 0
number_to_find = 1

def my_loop():
    global counter
    while counter > 6: #Also not sure while 7 doesn't work, says out of range?
        if numbers[counter] == number_to_find:
            print "Number found at position", counter 

    else:
        print "Counter not found in position" , counter
        counter = counter + 1
        my_loop()


print my_loop()
Run Code Online (Sandbox Code Playgroud)

python python-2.7

1
推荐指数
1
解决办法
76
查看次数

不理解python中的列表理解

在做一些列表理解练习时,我不小心做了下面的代码.这最终打印列表中所有16个条目的True/False.

threes_and_fives =[x % 3 == 0 or x % 5 == 0 for x in range(16)]
print threes_and_fives
Run Code Online (Sandbox Code Playgroud)

在我玩之后,我能够得到我想要的结果,它打印了该列表中可以被3或5整除的数字.

threes_and_fives =[x for x in range(16) if x % 3 == 0 or x % 5 == 0]
print threes_and_fives
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么第一个代码被评估为真或假而另一个没有?我正在努力掌握python所以更多的解释更好:)谢谢!

python python-2.7

1
推荐指数
1
解决办法
114
查看次数

标签 统计

python ×3

python-2.7 ×3

css ×1

html ×1

javascript ×1

jquery ×1