What is an instance in GUI programming

Create an instance of a class

In the last chapter we already created an instance of our class “BlueprintCatClass”. Here are the subtleties. Our previous code for the cat class:

And to create an instance (i.e. an object) we had the following call:

You can see the internal effects of the code extremely well when using the live editor (https://www.python-lernen.de/python-online-lern-editor.htm):

We have created the first instance - our first object is alive.

Let's look at other options.

Define default values ​​for properties

When creating a class, it would be useful to be able to define default values. So you could imagine that you always get the cats "freshly hatched". So the age would simply be 0.

This is exactly what we want to store as a default value for testing for age.

This is where our method comes into play again. We can extend properties with default values. You can, but you don't have to. In the example we only do it for the property "age":

The 0 is now stored as a standard argument for age internally in the memory:

If we create our instance (object) with an indication of the age, this is taken. This information always has priority.

However, if we do not enter anything when initializing, the default value is used. So we could also stipulate that most cats are black (you know that if you believe the saying).

If we now create a new object "katze_karlo" only with its nickname, it is assigned the default values:

We now have 2 cats:

And of course that is exactly why two cats were created somewhere in the world (and of course in your computer) at that very moment. I only say “high five”!

Important for default values ​​in classes

When assigning default values, the placement within. Our default values ​​always have to come at the end!

For example, if we only have the color as a default value and not the age, an error message appears. The following code goes wrong!

As an error message we then get: "SyntaxError: non-default argument follows default argument"

Recommend • Social Bookmarks • Thank you