Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-106625 : Add missing code to tutorial 4.6 example #106623

Merged
merged 6 commits into from
Jul 11, 2023

Conversation

RustyNail
Copy link
Contributor

@RustyNail RustyNail commented Jul 11, 2023

This is a pull request for the sample code in section 4.6.

I ran the following based on the sample code and got an error.

The sample code in the tutorial may be an excerpt of some code.
However, given that we will try "Point(1, var)" immediately following, I believe the user will easily understand that there is a necessary dataclass import statement.

Executed code :

class Point:
    x: int
    y: int

def where_is(point):
    match point:
        case Point(x=0, y=0):
            print("Origin")
        case Point(x=0, y=y):
            print(f"Y={y}")
        case Point(x=x, y=0):
            print(f"X={x}")
        case Point():
            print("Somewhere else")
        case _:
            print("Not a point")

## Added code
p = Point(1, 1)

Errors that occurred :

    p = Point(1, 1)
        ^^^^^^^^^^^
TypeError: Point() takes no arguments

📚 Documentation preview 📚: https://cpython-previews--106623.org.readthedocs.build/

@bedevere-bot bedevere-bot added awaiting review docs Documentation in the Doc dir skip news labels Jul 11, 2023
@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Jul 11, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@RustyNail RustyNail changed the title Added missing code from the sample code in chapter 4.6. gs-106625 : Added missing code from the sample code in chapter 4.6. Jul 11, 2023
@RustyNail RustyNail changed the title gs-106625 : Added missing code from the sample code in chapter 4.6. gh-106625 : Added missing code from the sample code in chapter 4.6. Jul 11, 2023
Doc/tutorial/controlflow.rst Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@terryjreedy terryjreedy dismissed ericvsmith’s stale review July 11, 2023 14:33

I made the requested change

@terryjreedy terryjreedy changed the title gh-106625 : Added missing code from the sample code in chapter 4.6. gh-106625 : Add missing code to tutorial 4.6 example Jul 11, 2023
@terryjreedy terryjreedy self-assigned this Jul 11, 2023
@terryjreedy terryjreedy added needs backport to 3.11 only security fixes needs backport to 3.12 bug and security fixes labels Jul 11, 2023
@methane
Copy link
Member

methane commented Jul 11, 2023

However, given that we will try "Point(1, var)" immediately following, I believe the user will easily understand that there is a necessary dataclass import statement.

That example is after the statement "You can use positional parameters with some builtin classes that provide an ordering for their attributes (e.g. dataclasses)."

Using dataclass before the statemnt is bit misleading.

In your case, following code would work.

p = Point()
p.x = 0
p.y = 42
where_is(p)

Of course, it is ugly. But the first example should demonstrate that named argument pattern like Point(x=y, y=y) will work for regular classes. So we shouldn't use dataclass here.

Instead, I think this makes the first example better.

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

With this Point class, user will understand that even typehint for attributes are not necessary for Point(x=x, y=y) pattern.

@terryjreedy
Copy link
Member

Reading @methane 's comment after fixing the example as a dataclass, I agree. The example follows "f you are using classes to structure your data you can use the class name followed by an argument list resembling a constructor, but with the ability to capture attributes into variables:". To me also, this implies a regular class with a contructor (__init__ method). The mention of dataclasses follows the example and I read that as applying to further examples.

@RustyNail
Copy link
Contributor Author

RustyNail commented Jul 11, 2023

Thanks for the review.
I will change this Pull Request change to initialization code according to the constructor using __init__.

@terryjreedy
Copy link
Member

terryjreedy commented Jul 11, 2023

@RustyNail I already made the change. Note that dunder names with double underscores need to be surrounded by backticks to display properly in GitHub comments. Otherwise the underscores are seen as markup. Compare 'init' with bolder 'init' with __init__.

@RustyNail
Copy link
Contributor Author

@terryjreedy
Thanks for the update.
I too understood your guys intent to use constructor initialization better.

@ericvsmith
Copy link
Member

Reading @methane 's comment after fixing the example as a dataclass, I agree.

I agree, too.

@terryjreedy terryjreedy merged commit d0b7e18 into python:main Jul 11, 2023
@miss-islington
Copy link
Contributor

Thanks @RustyNail for the PR, and @terryjreedy for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 11, 2023
…106623)

* Added missing import statement.

* Update Doc/tutorial/controlflow.rst

* Update Doc/tutorial/controlflow.rst

* Update controlflow.rst

* Make point regular class with __init__.

---------

(cherry picked from commit d0b7e18)

Co-authored-by: RustyNail <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
@bedevere-bot
Copy link

GH-106636 is a backport of this pull request to the 3.12 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 11, 2023
…106623)

* Added missing import statement.

* Update Doc/tutorial/controlflow.rst

* Update Doc/tutorial/controlflow.rst

* Update controlflow.rst

* Make point regular class with __init__.

---------

(cherry picked from commit d0b7e18)

Co-authored-by: RustyNail <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
@bedevere-bot bedevere-bot removed the needs backport to 3.12 bug and security fixes label Jul 11, 2023
@bedevere-bot
Copy link

GH-106637 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jul 11, 2023
terryjreedy added a commit that referenced this pull request Jul 11, 2023
… (#106637)

(cherry picked from commit d0b7e18)

Co-authored-by: RustyNail <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this pull request Jul 11, 2023
… (#106636)

(cherry picked from commit d0b7e18)

Co-authored-by: RustyNail <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants