Modules/PythonCrashcourse

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Fakhro
User
Beiträge: 21
Registriert: Mittwoch 13. März 2019, 13:44

Hi ich bräuchte mal eure hilfe undzwar versteh ich dass nicht ganz so richtig

8-17. Styling Functions:Choose any three programs you wrote for this chapter,
and make sure they follow the styling guidelines described in this section .

Undzwar solle ich meine funktionen stylen dies hab ich denke ich mal gemacht ist dies den richtig

def make_shirt(size, text='I like you'):
""" make a shirt """
print("I make a shirt with the size " + size.title())
print("I will say " + text.title())

make_shirt('medium')
make_shirt(size='large')

styling functions
Every function should have a comment that explains concisely what
the function does. This comment should appear immediately after the
function definition and use the docstring format. In a well-documented
function, other programmers can use the function by reading only the
description in the docstring. They should be able to trust that the code
works as described, and as long as they know the name of the function,
the arguments it needs, and the kind of value it returns, they should be
able to use it in their programs.
If you specify a default value for a parameter, no spaces should be used
on either side of the equal sign:

def function_name(parameter_0, parameter_1='default value')

The same convention should be used for keyword arguments in function calls:
function_name(value_0, parameter_1='value'

Dies versteh ich garnicht wie will es den gehen einfach so in der funktion einen wert einzugeben ohne eine zuweisung ?

mfg

fakhro
__deets__
User
Beiträge: 14536
Registriert: Mittwoch 14. Oktober 2015, 14:29

Bitte benutz in Zukunft die code Tags um den Code lesbar zu machen.

Und ich verstehe deine Frage nicht, bzw wie du auf die Interpretation kommst. Da WIRD doch ein Wert übergeben? Alles was die letzten Anmerkungen erwähnen ist, das keyword Argumente keine Leerzeichen um ihre Zuweisung haben sollen. Weder auf der aufrufenden Seite noch bei der Definition.

Code: Alles auswählen

def falsch(a = 3):

def richtig(a=3):

falsch(a = 10)
richtig(a=10)
Fakhro
User
Beiträge: 21
Registriert: Mittwoch 13. März 2019, 13:44

Also ich glaube du hast meine frage nicht so ganz verstanden
was ich damit meine ist

def function_name(parameter_0, parameter_1='default value')
bei jedem paramenter kann wen man möchte eine vorausgegebnen wert mit geben

Dass wäre jetzt für den code
def make_shirt(size, text='I like you'):
""" make a shirt """
print("I make a shirt with the size " + size.title())
print("I will say " + text.title())

make_shirt('medium')
make_shirt(size='large')

dass andere was ich garnicht verstanden habe ist dass:

function_name(value_0, parameter_1='value')

Hier wird ein wert in einer funktion eingeben und dann wird eine parameter gleich wert zuweisung mit gegeben
Ich meine wen dann kan mann doch nur einen parameter dazu geben mit voraus sage einem wert aber doch nicht wert und dann parameter gleich wert zu weisung hoffe verstehst was ich meine

Hier ist noch mal die ganz erklärung in Englich

styling functions
You need to keep a few details in mind when you’re styling functions.
Functions should have descriptive names, and these names should use
lowercase letters and underscores. Descriptive names help you and others
understand what your code is trying to do. Module names should use these
conventions as well.
Every function should have a comment that explains concisely what
the function does. This comment should appear immediately after the
function definition and use the docstring format. In a well-documented
function, other programmers can use the function by reading only the
description in the docstring. They should be able to trust that the code
works as described, and as long as they know the name of the function,
the arguments it needs, and the kind of value it returns, they should be
able to use it in their programs.
If you specify a default value for a parameter, no spaces should be used
on either side of the equal sign:
def function_name(parameter_0, parameter_1='default value')
The same convention should be used for keyword arguments in function calls:
function_name(value_0, parameter_1='value')
PEP 8 (https://www.python.org/dev/peps/pep-0008/ ) recommends that
you limit lines of code to 79 characters so every line is visible in a reasonably
sized editor window. If a set of parameters causes a function’s definition to
be longer than 79 characters, press enterafter the opening parenthesis on
the definition line. On the next line, press tabtwice to separate the list of
arguments from the body of the function, which will only be indented one
level.
Most editors automatically line up any additional lines of parameters to
match the indentation you have established on the first line:
def function_name(
parameter_0, parameter_1, parameter_2,
parameter_3, parameter_4, parameter_5):
function body...
If your program or module has more than one function, you can separate each by two blank lines to make it easier to see where one function
ends and the next one begins.
All importstatements should be written at the beginning of a file.
The only exception is if you use comments at the beginning of your file to
describe the overall program
__deets__
User
Beiträge: 14536
Registriert: Mittwoch 14. Oktober 2015, 14:29

nochmal: wenn du hier Code postest, benutz die Code Tags. Und was der Text beschreibt habe ich dir illustriert. Ist dir der Unterschied in meinen Beispielen klar? Es geht bei deinem Text einzig und alleine um Formatierung.

Was du beschreibst klingt danach, als ob du Sinn und Funktion von keyword arguments noch nicht durchdrungen hast. Lies/arbeite die entsprechende Lektion nochmal durch in deinem Kurs.
Fakhro
User
Beiträge: 21
Registriert: Mittwoch 13. März 2019, 13:44

Du hast meine frage immer noch nicht verstanden aber danke trotzdem
__deets__
User
Beiträge: 14536
Registriert: Mittwoch 14. Oktober 2015, 14:29

Doch, habe ich schon. Deine Frage bezieht sich auf die Semantik von Funktionsargumenten. Die hast du offensichtlich noch nicht vollständig verstanden. Wann man Argument weglassen kann, was es bedeutet den Argumentnamen VOR den Wert zu schreiben. Etc.

Dein Text bezieht sich nur nicht darauf. Er setzt das als verstanden voraus. Und redet nur über die Formatierung. Insofern findest du da auch keine Erklärung.

Hier ein Text der das probiert: http://treyhunner.com/2018/04/keyword-a ... -in-python
Fakhro
User
Beiträge: 21
Registriert: Mittwoch 13. März 2019, 13:44

function_name(value_0, parameter_1='value')


def make_shirt(size_0, text_1='i love you'):
"""make a shirt"""
print("I make a shirt with the size " + size_0)
print("It will say " + text_1.title())

make_shirt('medium')

RESTART: C:/Users/learn/AppData/Local/Programs/Python/Python37-32/trytest.py
I make a shirt with the size medium
It will say I Love You
>>>

wäre dass eig. richtig ? eine andere lösung hätte ich nicht für dieses beispiel
__deets__
User
Beiträge: 14536
Registriert: Mittwoch 14. Oktober 2015, 14:29

Und jedes mal ohne Code Tags 🙄
Benutzeravatar
__blackjack__
User
Beiträge: 13100
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Fakhro: Zu der Funktion: Der Docstring ist überflüssig. Genau wie bei Kommentaren, sollte für Docstrings gelten, das sie dem Leser einen Mehrwert liefern. Und die ”Dokumentation” """make a shirt""" bei einer Funktion die `make_shirt()` heisst, ist nichts was dem Leser irgend etwas bringt.

Im letzten Beitrag hast Du da Zahlen an die Namen angehängt: Bitte nicht! Was soll das?
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Fakhro
User
Beiträge: 21
Registriert: Mittwoch 13. März 2019, 13:44

Dass habe ich schnell mal so gemacht nur um zu fragen ob es richtig wäre ... dass mit den zahlen wirst du verstehen wen du oben liest was ich meine
deets wie meinst du dass ohne code tags code tags sind doch dazu da um den code zu beschreiben wie z.B in meine code erwähnt der docstring oder auch #
__deets__
User
Beiträge: 14536
Registriert: Mittwoch 14. Oktober 2015, 14:29

Code Tags ist ein Feature hier im Forum. Damit die in Python eminent wichtigen Einrückungen nicht verloren gehen. Dazu gehst du in den vollständigen Editor hier. Markierst den Code. Und drückst </>
Benutzeravatar
__blackjack__
User
Beiträge: 13100
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Fakhro: Nein, das mit den Zahlen verstehe ich nicht und die haben da auch nichts zu suchen.

Mit Code-Tags ist die Auszeichnung hier im Forum gemeint, damit bei dem Code den Du postest die Einrückung erhalten bleibt. Die ist bei Python ja wichtig zum Verständnis des Codes. Der Code in Deinem ersten Beiträg sähe zum Beispiel so viel besser aus:

Code: Alles auswählen

def make_shirt(size, text='I like you'):
    """ make a shirt """
    print("I make a shirt with the size " + size.title())
    print("It will say " + text.title())

make_shirt('medium')
make_shirt(size='large')
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Antworten