class Parser():
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
def html_escape(text):
return "".join(html_escape_table.get(c,c) for c in text)
def query():
database = [x, y, z, etc, etc2]
for x in database:
html_escape(x)
print x #for testing purposes
return
test = Parser()
test.query()
Am I doing this correctly? I keep getting an error:
TypeError: query() takes no arguments (1 given)
I’m not seeing anywhere where I am passing an argument to query, or even to Parser.
Can someone explain what I am doing wrong here?
I tried calling just Parser.query() and got this error (this was after adding the self argument to all of my functions and object argument to my Parser class)
Parser.query()
TypeError: unbound method query() must be called with Parser instance as first argument (got nothing instead)
Methods in classes require the argument
selfthis is to do with parsing the instance to the method in how python does instance methods.e.g.
Just to expand on this if you want to parse arguments to an instance method you still need
selfEdit:
Okay to explain it further you have to know how python handles instances, python handles instances in a very verbose and clear way,
selfis always used to refer to itself or this current instance, just likethisin Java. So when python callsmy_instance.method()its actually callingTheObject.method(my_instance)hence why self refers to themy_instanceinside the method. That allows you to use the instance inside the method with the instance itself passed around inside the arugments.Edit 2:
Even when you have self as an argument to the methods you need to call it from an instance like so
Edit 3:
This isn’t Java you don’t have to bind your functions together as methods inside a class, just leave them as free-roaming functions inside a parser.py file then you can do