0

I am trying to test my code by calling the lint() method and passing '{[()]}' as the text argument. I am not familiar with classes, and I am getting TypeError: lint() missing 1 required positional argument: 'text'.

class Stack: def __init__(self): self.stack = [] def push(self, element): self.stack.append(element) def pop(self): if len(self.stack) == 0: return None else: return self.stack.pop() def read(self): if len(self.stack) == 0: return None else: return self.stack[-1] class Linter: def __init__(self): # We use a simple array to serve as our stack: self.stack = Stack() def lint(self, text): # We start a loop which reads each character in out text: for t in text: braces = {'(': ')', '[': ']', '{': '}'} # If the character is an opening brace: if t == '(' or t == '[' or t == '{': # We push it onto the stack: self.stack.push(t) # If the character is a closing brace: elif t == ')' or t == ']' or t == '}': # Pop from stack: popped_opening_brace = self.stack.pop() # If the stack was empty, so what we popped was None, # it means that an opening brace is missing: if not popped_opening_brace: return f'{t} doesnt have opening brace' # If the popped opening brace doesn't match the # current closing brace, we produce and error: if t != braces[popped_opening_brace]: return f'{t} has a mismatched opening brace' # If we get to the end of line, and the stack isn't empty: if self.stack.read(): # It means we have an opening brace without a # corresponding closing brace, so we produce an error: return f'{self.stack.read()} does not have a closing brace' # Return true if line has no errors: return True Linter.lint('{[()]}') 

Linter.lint(self, '{[()]}') doesn't work either.

4
  • you have to "instantiate" your Linter first and call the method on the instance, rather than on the class directly... i.e. mylinter = Linter() mylinter.lint('{[()]}') Commented Jul 27, 2022 at 13:21
  • you also need to instantiate the Stack here: self.stack = Stack like self.stack = Stack() Commented Jul 27, 2022 at 13:22
  • have a read some info about how classes work realpython.com/courses/… and realpython.com/instance-class-and-static-methods-demystified Commented Jul 27, 2022 at 13:23
  • If you want the method to be a class method you will need to declare it using the @classmethod decorator. The implication of that is that your method will not be able to access self, so you will need to update your method to not need that. Commented Jul 27, 2022 at 13:34

1 Answer 1

0

You are not instantiating an element of both Linter and Stack classes. You can't just use methods of classes out of the blue without creating an instance of the object,since the self parameter that's passed is a reference to the instance itself. Try declaring l=Linter() and then l.lint('{[()]}'). If you want to use methods without needing to make an instance of the class, you should read about static methods.

Sign up to request clarification or add additional context in comments.

4 Comments

"You can't just use methods of classes out of the blue without creating an instance of the object" is false, otherwise the built-in decorator @classmethod would not exist. (However, I can see that the OP probably doesn't really want a class method.)
Which is pretty much the same of @staticmethod, therefore not needing an instance of the object. Nobody said "YOU LITERALLY CANNOT USE METHODS WITHOUT INSTANTIATING EVER BRO", I already invited op to read about static methods if that's what he wants to achieve.
The difference between @staticmethod and @classmethod is that @classmethodcan modify the class state by changing the value of a class variable. So not "pretty much the same."
That is correct, but you're expanding a concept nobody asked for just for the sake of showing off, while I was just trying to point op in the right direction. I didn't even bring up @ classmethod and @staticmethod in the discussion because of how verbose it would've been (and already has).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.