Hallo.
Normalerweise entwickle ich in ** C # **, aber kürzlich hatte ich die Gelegenheit, mit ** Python ** in Kontakt zu treten. Nach dem Codieren mit dem Gefühl von ** C # **, als ich erneut mit PEP8 nachgesehen habe, dem Codierungsstandard von ** Python **, der Variablendeklaration usw. Übrigens gab es einen Unterschied in den Namensregeln, daher habe ich die Namensregeln für ** C # ** und ** Python ** grob zusammengefasst.
In PEP8 wird geschrieben, dass das Projekt dem Codierungsstandard Vorrang einräumen sollte, wenn der Codierungsstandard des Projekts wie folgt in Konflikt steht.
Viele Projekte haben Richtlinien für ihren Codierungsstil. Wenn es mit den Bestimmungen dieses Dokuments in Konflikt steht, haben die Richtlinien für dieses Projekt Vorrang.
Zunächst eine kurze Erläuterung der Namenskonvention.
Namensregeln | Erläuterung | Beispiel |
---|---|---|
CamelCase | Großschreibung des Wortanfangs außer des Anfangs. | happyNewYear |
PascalCase | Großschreibung des Wortanfangs einschließlich des Anfangs. | HappyNewYear |
SnakeCase | Alle Wörter sind in Kleinbuchstaben. Verbinden Sie sich mit einem Unterstrich. | happy_new_year |
ConstantCase | Alle Wörter sind Kapital. Verbinden Sie sich mit einem Unterstrich. | HAPPY_NEW_YEAR |
Eine einfache Vergleichsliste der C # - und Python-Namenskonventionen.
Kennung TH> | C # TH> | Python TH> TR> |
---|---|---|
Paket (Namespace) TD> | PascalCase TD> | Alle Kleinbuchstaben TD> TR> |
HappyBirthday | happybirthday | |
Modul TD> | PascalCase TD> | Alle Kleinbuchstaben / SnakeCase TD> TR> |
HappyBirthday | happybirthday / happy_birthday | |
Klasse TD> | PascalCase TD> | PascalCase TD> TR> |
HappyBirthday | HappyBirthday | |
Typvariable TD> | PascalCase TD> | PascalCase TD> TR> |
HappyBirthday | HappyBirthday | |
Ausnahme TD> | PascalCase TD> | PascalCase TD> TR> |
HappyBirthdayException | HappyBirthdayError | |
Globale Variablen TD> | Pascal TD> | Schlangenfall TD> TR> |
HappyBirthday | happy_birthday | |
Parameter (Argumente) TD> | CamelCase TD> | SnakeCase TD> TR> |
happyBirthday | happy_birthday | |
Methode (Funktion) TD> | PascalCase TD> | SnakeCase TD> TR> |
GetHappyBirthday | get_happy_birthday | |
Variablen TD> | CamelCase TD> | SnakeCase TD> TR> |
happyBirthday | happy_birthday | |
Konstante TD> | PascalCase TD> | ConstantCase TD> TR> |
HappyBirthday | HAPPY_BIRTHDAY |
Darüber hinaus ist der Unterschied in der Beschreibung wie Klassendefinition und Methodendefinition in der Steueranweisung wie Einzug, Kommentar, IF usw. in PEP8 unterschiedlich. Ich werde die Gelegenheit nutzen, um es zusammenzufassen.
Referenz) Python Code Style Guide (PEP8)