小编cai*_*ing的帖子

如何使类属性专属于超类

我有一个星球大师班:

class Planet:

    def __init__(self,name):
        self.name = name
        (...)

    def destroy(self):
        (...)
Run Code Online (Sandbox Code Playgroud)

我还有一些继承的类,Planet我想让其中一个无法被销毁(不继承该destroy函数)

例:

class Undestroyable(Planet):

    def __init__(self,name):
        super().__init__(name)
        (...)

    #Now it shouldn't have the destroy(self) function
Run Code Online (Sandbox Code Playgroud)

所以当这个运行时,

Undestroyable('This Planet').destroy()
Run Code Online (Sandbox Code Playgroud)

它应该产生如下错误:

AttributeError: Undestroyable has no attribute 'destroy'
Run Code Online (Sandbox Code Playgroud)

python oop inheritance class python-3.x

13
推荐指数
3
解决办法
861
查看次数

导入此目的

Python中有一个众所周知的Easter Egg import this,当你添加到代码中时会自动输出

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and …
Run Code Online (Sandbox Code Playgroud)

python module

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

如何排序列表,只排序字符串?

我有一个字符串和整数列表,并希望对列表进行排序,保留这样的数字

["Hello", 1, 2, "World", 6, "Foo", 3]
Run Code Online (Sandbox Code Playgroud)

会成为

["Foo", 1, 2, "Hello", 6, "World", 3]
Run Code Online (Sandbox Code Playgroud)

简而言之,它只对列表中的字符串进行排序,而不是整数,它们保留在原位.我已经尝试使用key参数,list.sort()但没有设法实现我想要的.

谁能帮我这个?

编辑:这与链接的问题不同,因为我想保留整数的索引,而不是将它们与字符串一起排序.

编辑:这与第二个链接的问题不同,因为该问题的答案可以使用key参数解决问题,我明确声明的内容在此实例中不起作用.

python sorting list

5
推荐指数
2
解决办法
271
查看次数

如何计算字符串开头的字符数?

如何计算Python中字符串开头/结尾的字符数?

例如,如果字符串是

'ffffhuffh'
Run Code Online (Sandbox Code Playgroud)

我如何计算字符串开头fs 数?上面的字符串应该输出4.f

str.count 对我来说没有用,因为角色可能在字符串的中间.

python string count python-3.x

4
推荐指数
2
解决办法
3533
查看次数

对具有保持在固定位置的特定值的列表进行排序

我有一个字符串列表.我只想对符合特定条件的值进行排序.考虑这个清单

['foo','bar','testa','python','java','abc']
Run Code Online (Sandbox Code Playgroud)

我只想用其中的值对值进行排序a.结果应该是这样的

['foo','abc','bar','python','java','testa']
Run Code Online (Sandbox Code Playgroud)

具有的元素a将适当地改变位置,但其他元素保留其原始位置.

我完全不知道如何实现这一点,所以我希望其他人这样做.有人能告诉我怎么做吗?

python sorting algorithm python-3.x

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

标签 统计

python ×5

python-3.x ×3

sorting ×2

algorithm ×1

class ×1

count ×1

inheritance ×1

list ×1

module ×1

oop ×1

string ×1