出于组织目的在 Python 代码中强制缩进

发布于 2024-12-20 22:17:54 字数 561 浏览 2 评论 0原文

Python 有没有办法强制缩进? 我想要它是为了让代码看起来更有条理。 举个例子:

# How it looks now
class Bro:
    def __init__(self):
        self.head = 1
        self.head.eye = 2
        self.head.nose = 1
        self.head.mouth = 1
        self.neck = 1
        self.torso = 1

# How it'd look ideally (indenting sub-variables of 'head')
class Bro:
    def __init__(self):
        self.head = 1
            self.head.eye = 2
            self.head.nose = 1
            self.head.mouth = 1
        self.neck = 1
        self.torso = 1

我想通过某种解决方法这是可能的,是吗?

Is there a way to force an indent in Python?
I kind of want it for the sake of making the code look organized.
As an example:

# How it looks now
class Bro:
    def __init__(self):
        self.head = 1
        self.head.eye = 2
        self.head.nose = 1
        self.head.mouth = 1
        self.neck = 1
        self.torso = 1

# How it'd look ideally (indenting sub-variables of 'head')
class Bro:
    def __init__(self):
        self.head = 1
            self.head.eye = 2
            self.head.nose = 1
            self.head.mouth = 1
        self.neck = 1
        self.torso = 1

I imagine this is possible with some sort of workaround, yeah?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清秋悲枫 2024-12-27 22:17:54

你不能做这样的缩进,因为缩进用于解析Python代码块,而不仅仅是出于时尚的原因......

如果你想以某种方式对它们进行分组,您可以按如下方式使用字典:

class Bro:
    def __init__(self):
        self.head {
            'head': 1,
            'eye' : 2,
            'nose' : 1,
            'mouth' : 1,
        }
        self.neck = 1
        self.torso = 1

但这不是一个好方法,因此在字典中定义所有身体部位更好

class Bro:
    def __init__(self):
        self.body {
            'head': {
                'head': 1,#defining itself
                'eye' : 2,
                'nose' : 1,
                'mouth' : 1,
            },
            'neck' : 1,
            'torso' : 1, 
       }

可能是更好的方法,但它更难使用,因为编写 self.nose 是比简单self.head.noseself.body['head']['nose']

You can not make a such indentation, because indentatin is used for parsing python code blocks, not simply just for stylish reasons...

If you want to group them somehow, the you can use a dict as follows:

class Bro:
    def __init__(self):
        self.head {
            'head': 1,
            'eye' : 2,
            'nose' : 1,
            'mouth' : 1,
        }
        self.neck = 1
        self.torso = 1

but simply that is not a good way, so defining all body parts within dictionary is better

class Bro:
    def __init__(self):
        self.body {
            'head': {
                'head': 1,#defining itself
                'eye' : 2,
                'nose' : 1,
                'mouth' : 1,
            },
            'neck' : 1,
            'torso' : 1, 
       }

Might be a better approach, but its harder to use since writing self.nose is simpler than self.head.nose or self.body['head']['nose']

奢欲 2024-12-27 22:17:54

从技术上讲,是的,这是可能的,例如:

class Bro:
    def __init__(self):
        self.head = 1
        if True:
            self.head.eye = 2
            self.head.nose = 1
            self.head.mouth = 1
        self.neck = 1
        self.torso = 1

就可读性而言,这是否是一个好主意,我不确定。我个人可能不会这样做。

PS One 可能会使用(滥用?)with 语句来使其可读性更好。

Technically, yes it's possible, e.g.:

class Bro:
    def __init__(self):
        self.head = 1
        if True:
            self.head.eye = 2
            self.head.nose = 1
            self.head.mouth = 1
        self.neck = 1
        self.torso = 1

Whether that's a good idea as far as readability goes, I am not sure. I personally would probably not do it.

P.S. One could probably use (abuse?) the with statement to make it read better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文