I'm studying python classes, but I don't know what's wrong.

Asked 2 years ago, Updated 2 years ago, 28 views

I am currently studying python on my own, but I used everyone's python (fourth edition) and typed it in as below, but they didn't respond according to the reference book.Please tell me where I'm wrong and what I should do. (Python's version is 3.6.5 and I'm practicing using Anaconda's jupyter notebook.)

In(1)class Prism:
        def_init_(self, width, height, depth):
            self.width = width
            self.height=height
            self.depth = depth
        def content(self):
            return self.width*self.height*self.depth
In(2)p1 = Prism(10,20,30)
     p1.content()

The reference book says that the return value is 6000, but

--->1p1 = Prism(10,20,30)
      2 p1.content()

TypeError: object() takes no parameters

There will be an error.Please let me know.

python

2022-09-30 21:35

1 Answers

Invalid constructor definition.

class Prism:
     def_init__(self, width, height, dept):
         self.width = width
         self.height=height
         self.depth=dept
     def content(self):
         return self.width, self.height, self.depth

 p1 = Prism(10,20,30)

Constructors in python must be defined as def_init__.definit is not treated as a constructor, so I went to the constructor of the base class Object to find three patterns, but it was an error.


2022-09-30 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.