Python class 2 oops

class A:
def init(self,name):
self.name=name
print(self.name,name)
def hi(int(regd)):
print(regd)

ajay=A(“siddartha”)
ajay.hi(11234)

why the code is giving error?
and when we assign name to self.name…why its not giving error that name is not declared in the class

hey @ajaysiddartha .
there are some mistakes that you have made while implemeting it.

  1. every function inside a class needs to have a parameter self as the first parameter , so that it can get reference of the particular while being called by the object of that class.
  2. You can’t use like int the you have used in function parameter.
    if still you want you can do it like , def hi(regd : int) , this confirms that the parameter will be a integer value.

It works becuase , there are multiple way to initialize the variables in python.
You could have also declared these variable before __init__ , but it means the same and hence it works and doesn’t give any error.

i hope you understand this.
Thank You :slightly_smiling_face:.

so the methods will not work without self as an arguement

The method will work as a local function inside that class , and then it can only be used inside it.
So , if you to call using any object , then it wont work.