+ 2
19 odpowiedzi
+ 6
So what?
Take a break.
Example:
https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/gloss_python_class_init.asp
+ 8
Take a break, watch the second half of the tutorial, then clarify what you want to do.
+ 3
What is it supposed to do?
You define a class but never create an object.
Which output do you expect?
+ 2
In python Classes, the __init__() method initialize any member inside it. Its first argument must be self, and you need to name the member in its paramtheses.
class test:
def __init__(self,name,age):
self.name = name
self.age = age
With __init__(), when you instantiate an object(of the class) outside the class, you can pass the "parameter" to the object.
Also, when calling member that has been initialized in the __init__() method(with self), you have to put "self.", With the member name.
class Car:
def __init__(self,name,age):
self.name = name
self.age = age
def showInfo(self):
print("Name: ",self.name,""\nAge:",self.age)
car1 = Car("Mercedes car", 2000)
car1.showinfo()
+ 2
# with __init__ method can be defined a default object
class car:
def __init__(self, brand):
self.brand=brand
def about_car(self):
print(self.brand)
car_1 = car('Ferrari')
mercedes = car('Mercedes')
car_1.about_car()
+ 1
It's on pc.
+ 1
Podle Playz When you'd use the __init__() method? With __init__() method, you can declare your own variable here, and it can be accessed by the method of the class! So it is always a good practice to add and put the __init__() method in the top of the class.. you can also create a variable inside it WITHOUT self(a variable that is created and can be used to serve class stuff),but DON'T pass it to the parantheses of the __init__() method as parameter
In my opinion, "self' refers to the "parameter" passed to the class.. I don't know if it's correct, since I'm not familar with Python classes..
+ 1
Ok thx
+ 1
U need help or u have solved it?
+ 1
The provided code defines a class named car with a constructor (__init__ method) that takes a parameter called make. The constructor initializes an instance variable make with the value passed to it.
To create an object of the car class and assign it to the variable x with the make "Honda," you can use the following code:
python
Copy code
class car:
def __init__(self, make):
self.make = make
x = car("Honda")
To access the make attribute of the x object, you can use the dot notation (x.make) and print it as follows:
python
Copy code
print(x.make)
When you run the complete code, it will output:
Copy code
Honda
This means the make attribute of the x object is set to "Honda" and successfully printed using the print statement.
+ 1
S2XPHOENIX solved
+ 1
Podle Playz okie gud
+ 1
Mc Rey Pangandoyon Quiao Thanks
0
I don't know how to use __init__
0
Please show
0
I watched half a tutorial before giving up, that's why I'm here 😂
0
I'm just gonna doodle around with tutorials lol
0
Dragon RB thanks, I know all that I just don't understand why and when you'd use that. Still very helpful!