#๐Ÿ”’ Need Definitions and answers to some questions

12 messages ยท Page 1 of 1 (latest)

languid igloo
#

can someone explain to me the following terms:

  1. Instance
  2. Object

what exactly does the self parameter do in class definition and what does the _ init_() method do?

daring crownBOT
#

@languid igloo

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.

cedar wagon
#

instance and object are roughly the same thing, you get them from instantiating a class. A class is like a blueprint, an instance is like a house built from the blueprint. self is a special name that always refers to the current instance.

#

in Python specifically, nearly everything is an object. 42 is an object, it's an instance of class int

languid igloo
cedar wagon
#

__init__ is for initializing an object, once the object exists, __init__ is called to allow you to setup its state. For example, if you have DatabaseConnection class, in __init__ you might try to connect to the server, set the properties like self.address = "localhost" or what not

#

You don't have to implement __init__ for a class if it doesn't need any special setup and just holds data (like dataclasses for example (even though the __init__ will be hidden and actually do some work))

languid igloo
#

thank you

#

!close

daring crownBOT
#
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.