Anonymous Quiz
30%
list()
45%
[ ]
6%
Same speed
19%
Depends on list size
Anonymous Quiz
15%
set
13%
dict
58%
array
13%
tuple
Feels off?
It's because all your objects share one variable (without you realizing it)
At first glance, it seems like every object should start fresh, right? But in this case, count is a class variable, which means it’s shared by all instances of the class.
Every time you create a new
If your goal is to give each object its own value, define it like this instead
Now, each instance has its own count, stored on the object itself . no sharing, no surprises.
It's because all your objects share one variable (without you realizing it)
At first glance, it seems like every object should start fresh, right? But in this case, count is a class variable, which means it’s shared by all instances of the class.
Every time you create a new
Counter()
, you’re actually incrementing the same shared variable not something unique to each object.If your goal is to give each object its own value, define it like this instead
class Counter:
def __init__(self):
self.count = 1
Now, each instance has its own count, stored on the object itself . no sharing, no surprises.
❤2
❤3
Enjoy our content? Advertise on this channel and reach a highly engaged audience! 👉🏻
It's easy with Telega.io. As the leading platform for native ads and integrations on Telegram, it provides user-friendly and efficient tools for quick and automated ad launches.
⚡️ Place your ad here in three simple steps:
1 Sign up
2 Top up the balance in a convenient way
3 Create your advertising post
If your ad aligns with our content, we’ll gladly publish it.
Start your promotion journey now!
It's easy with Telega.io. As the leading platform for native ads and integrations on Telegram, it provides user-friendly and efficient tools for quick and automated ad launches.
⚡️ Place your ad here in three simple steps:
1 Sign up
2 Top up the balance in a convenient way
3 Create your advertising post
If your ad aligns with our content, we’ll gladly publish it.
Start your promotion journey now!
❤1
Looks neat, right? But let’s slow down.
The
The starred variable always gets a list, and it can be empty so plan accordingly when unpacking, especially in function arguments or loops.
For example: Consider the following code
No error, but
The
*b
syntax is called extended iterable unpacking. It grabs everything in the middle of the list, leaving the first item (a) and the last (c) outside the star. This pattern is super handy, but can also behave unexpectedly if you assume it’ll grab just one item or not consider the structure of the data.The starred variable always gets a list, and it can be empty so plan accordingly when unpacking, especially in function arguments or loops.
For example: Consider the following code
x, *y = [42]
print(y) # []
No error, but
y
is just an empty list! Unpacking doesn’t always fill every name the way you might guess.❤1👍1