User made classes

Hi.
I’m having a bit of trouble understanding the code below. I know when it’s run it will produce x=2 and y=2. But i don’t understand how.

class Points(object):
def init(self,x,y):

self.x=x
self.y=y

def print_point(self):

print('x=',self.x,' y=',self.y)

p2=Points(1,2)

p2.x=2

p2.print_point()

Thanks

What, specifically, is unclear to you? “I don’t understand how this code does what it does” is too broad a question.

Perhaps check out the official tutorial, it might clear things up.

I’m not understanding why it returns x=2 and y=2.

It doesn’t. It prints ‘x=2 y=2’. Printing is not the same as returning.

I will try to explain.

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

    def print_point(self):
        print('x=',self.x,' y=',self.y)

This part defines a class called Points. It says that a Points has two attributes, x and y, which must be provided whenever an instance of Points is created.

Upon creation of a Points instance, the __init__ method runs automatically and sets the attributes x and y to the provided values. These attributes can be accessed from within the class itself as self.x and self.y.

The print_point method makes use of this by accessing and printing the values held in the x and y attributes.

p2=Points(1,2)
p2.x=2
p2.print_point()

This part creates a Points instance called p2, whose attributes are initially set to x=1 and y=2. The instance’s x attribute is then changed from 1 to 2. Finally, the current values of these attributes are printed. Since y was initialized to 2 and x was changed to 2, they are printed as ‘x=2 y=2’.

I hope that clears up any confusion.

The line p2 = Points(1, 2) creates a “Points” object with two attributes. The “x” attribute is set to 1, and the “y” attribute is set to 2.

The line p2.x = 2 changes the “x” attribute from its old value of 1 to the new value 2.

The line p2.print_point() then calls the print_point method, which prints a message “x= VALUE y= VALUE” where the two values are taken from the object’s “x” and “y” attributes, which by this time are both 2.

This might be more clear to you if:

  • you look at the object both before and after changing it;
  • you use more distinctive values;
  • and you compare two or more objects, not just one.

By the way, please format your code using “code fences”, or three
backticks on a line of their own, like this:

code goes between the code fences

Okay, let’s get back to your Points class.

That’s a poor name, because it is plural. Your class doesn’t represent more than one Point (Points, plural), it represents a single Point. So you should name it Point, not Points.

Once you have renamed your class to Point, let’s do this:

p = Point(10, 20)
p.print_point()  # Should display x= 10 y= 20
p.x = 44
p.print_point()  # Should display x= 44 y= 20
p.y = 15
p.print_point()  # Should display x= 44 y= 15

Is that more clear what is going on? Now let’s try a second object.

q = Point(77, 99)
q.print_point()  # Should display x= 77 y= 99
p.print_point()  # Should display x= 44 y= 15

So p and q are different objects, each with their own pair of x and y attributes.

Hope this helps.

Ah crap, here we go again, yet again this Discourse software disappoints with its bugs…

I tried to explain how to put code in between code fences, but the backticks disappear on the web site.

Let’s try again.

Here is a backtick: `

It is usually found on the keyboard under the ~ key next to the 1 key.

To format your code as code, so it keeps it formatted correctly, put it in between “code fences” made up of three backticks ```.

The code fences must be on their own lines, with the code in between them.

\`\`\`
code goes here
\`\`\`

(That should be three backticks, a new line, code, new line, three backticks.)

If this post doesn’t display correctly, I’m out of ideas. Sorry.

By Steven D’Aprano via Discussions on Python.org at 04Aug2022 22:28:

I tried to explain how to put code in between code fences, but the
backticks disappear on the web site.

In your previous post it appears that your:

```
backticked code here
```

example was indented by only 3 spaces, while MarkDown requires 4 for
preformatted code. So I suspect you were in “indented prose” mode (to
invent a term) and the backticks are taken as font markers.

Cheers,
Cameron Simpson cs@cskk.id.au

Code blocks can be created by 4-space indents to suppress the markdown and here’s another way:

  • use a combination of block quote and escape characters.

>\```
\<code here>
\```
#angle brackets also escaped to suppress HTML parsing.

…produces a faithful facsimile of what the editor will show:

```
<code here>
```

This took a fair amount of trial and error to work out, though, and is clumsy. It’s much simpler to indent and let Discourse render the text in monospace as Cameron recommended. The code block also scrolls and provides rhe ‘copy code’ button, which can be useful.

```
<code here>
```

Oops. Thanks for picking that up.