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
- How do I get AdSense in Blogger
- How is Oracle Hyperion Essbase as a carrier
- What makes me get up early?
- The Apple card charges interest
- Why none of the onion links work
- What is your opinion on Lakewood guitars
- Why is SoftBank crazy about startups
- How often do earthquakes happen in Idaho
- Kill a butterfly. Bad luck
- What are your top 5 lipsticks
- What does 10 mean spiritually
- What are three-phase electrical systems
- Cell phones create distractions
- How do I get a prepaid Mastercard
- How did Christianity come to Senegal
- Should I buy Saturn VUE in 2007?
- What is puttu
- What is the best relationship guide
- Colorado people wear cowboy boots
- Where does gluconeogenesis take place
- Neutering a cat calms its personality
- How many sides does a polygon have
- How can someone be found on Snapchat
- Why do loud noises make me angry