(I am new to Python, and I am not very good at OOP programming in general)
I have made Python programs before (Like maybe 6 or 7 small programs to test features), but I have always stayed in one file, i.e. making a class in that one Python file, and making a instance of it in the same file, and calling functions outside the classes scope. I never had to pass any arguments to any function that only had "self" as a parameter. But now I have this class here: main.py:
#"main.py" is the main script of the application; The application executes this script first;#
#IMPORT FORMAT: from FILENAME import CLASSNAME
from getBoundsForApplicationUse import getBoundsForApplicationUse; #Class is part of application.#
#Variables#
connectOrCreateServer = 2 #Will be updated with users choice; 0 = choose to host/create server; 1 = choose to connect as a client to an existing server;#
#Entry Point#
getBoundsForApplicationUse_ = getBoundsForApplicationUse; #Create instance of class "getBoundsForApplicationUse"; Check top of that specific class for information;#
getBoundsForApplicationUse_.welcomeUser(''); #Welcome the user to the application in IOStream#
As you can see, I am creating an instance of class "getBoundsForApplicationUse" from a file named "getBoundsForApplicationUse.py" (Code for that class below), which said instance is named "getBoundsForApplicationUse_" (Notice the underscoe '_' at the rear of the instance name, class and instance name are NOT identical). From this point I am calling function "welcomeUser()", which only has 'self' as a parameter: getBoundsForApplicationUse.py:
#"getBoundsForApplicationUse.py", will query the user, getting information on server or client connection for the IRC to connect; I.e, this script will get IP Address, Port Number, ask to start a server, ask to start a client, etc.#
class getBoundsForApplicationUse:
#This function allows the user to choose to connect to a server, or host one; Should return value back to variable "connectOrCreateServer" in "main.py"; 0 = choose to host/create server; 1 = choose to connect as a client to an existing server;#
def userChooseOption(self):
return
#This function welcomes the user to the application; Message created in IOStream#
def welcomeUser(self):
print("Welcome to ViprinIRC! A lightweight IRC chat!\n")
return
I have to pass '', or any string of text, or else I get this error: "Traceback (most recent call last): File "E:\X-Viprin\GameCreation\AllProjects\Projects\Python\ViprinIRC\main.py", line 12, in getBoundsForApplicationUse_.welcomeUser(); #Welcome the user to the application in IOStream# TypeError: getBoundsForApplicationUse.welcomeUser() missing 1 required positional argument: 'self'"
I know that the keyword 'self' is quiet similar to keyword 'this' from java. I can use 'self' to refer to class variables and methods from this class, and other classes. For example, I could make a constructor, and I could pass 3 different integer arguments to that constructor. From there, I could use the 'self' keyword, to set the recieved arguments to class variables: "self.myClassVariableOne = myRecievedArgumentOne". I have some idea of the self keyword, but why am I needing to pass a string to the self keyword, or is something else going one, which I am not realizing? I looked up some tutorials, and entered the error into a search engine, and had trouble finding any solution.
Could someone explain what is happening here? If you can, then thank you.
....................
Your problem is here:
getBoundsForApplicationUse_ = getBoundsForApplicationUse; #Create instance of class "getBoundsForApplicationUse";
This doesn't create an instance of your class, it just creates another reference to the class itself.
You should be doing
getBoundsForApplicationUse_ = getBoundsForApplicationUse(); #Create instance of class "getBoundsForApplicationUse"
getBoundsForApplicationUse_.welcomeUser()
Once you've done this, welcomeUser()
will receive your instance automatically as the self
parameter.
Keep in mind, it was never necessary to pass a string in your code. Try passing 5, or an array. It will also work! This is because welcomeUser()
is very simple and only requires you to pass something as self
, but self
would usually be expected to be an instance of the class.