#๐Ÿ”’ VSCode not recognizing attribute?

16 messages ยท Page 1 of 1 (latest)

supple mulch
#

Its not recognizing the next_node on current.next_node

tall sapphireBOT
#

@supple mulch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

austere zodiac
#

Since you only ever assign None to self.head, it probably inferred that the type of self.head is None. Try changing the initial assignment to:

self.head: Node | None = None
#

Also, you should get rid of the assignments at the top of the Node class. That is giving the class itself data and next_node attributes.

#

@supple mulch

#

Actually, problem 2 might be the real problem. Node instances do not have a next_node attribute.

#

I'd try both fixes and see which helps.

supple mulch
austere zodiac
#

The first issue isn't really a class problem. Since you only ever assigned None to self.head and didn't specify the type of self.head, VSCode had to guess the type. Since you did self.head = None, it made the reasonable guess that the type of self.head is None.

Later, you do current = self.head, which means that current is likely the same type as self.head, so if self.head has the type None, so does current.

Then later you do current.next_node, which doesn't make sense if current is None, since None doesn't have an attribute next_node.

#

: Node | None says "The type of this is either a Node, or None".

supple mulch
#

I see thanks, this stuff is kind of confusing to wrap my head around, apprecaite the help

austere zodiac
#

Types can be a bit of a pain and hard to wrap your head around, but they also help catch mistakes. They're well worth the effort to learn.

And you're welcome.

#

If you really want to learn static typing, learn a language like Haskell or Typescript that have deep type systems.

#

Python's implementation of typing is pretty weak, and is really only used by the editor.

tall sapphireBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.