type annotation vs working code

Karsten Hilbert Karsten.Hilbert at gmx.net
Sun Oct 1 07:57:10 EDT 2023


Sorry for having conflated the core of the matter with all
the Borg shenanigans, that's where I found the problem in my
real code, so there :-)

Consider this:

#----------------------------------------------------
class Surprise:
	def __init__(self, with_type_annotation=False):
		if with_type_annotation:
			try:
				self.does_not_exist:bool
				print('does_not_exist does exist')
			except AttributeError:
				print('does_not_exist does not exist')
			return

		try:
			self.does_not_exist
			print('does_not_exist does exist')
		except AttributeError:
			print('does_not_exist does not exist')

Surprise(with_type_annotation = False)
Surprise(with_type_annotation = True)
#----------------------------------------------------

Is this how it is supposed to be ?


> ...and so we're addressing the important question: the try-test is for existence, cf for
> some value.
>
> This can also be achieved by using the attribute in a legal expression, eg:
...
> Might this remove the confusion (ref: @Mats):
>
>     self.already_initialized:bool == True

Not for me as that would _create_ already_initialized on the
instance. It would not allow me to test for it.

> >Which seems akin constructs for generating compatibility
> >between versions.
>
> versions of ?

Of the language. Sometimes one tests for existence of a given
class in a module and defines said class oneself if it does
not exist. But that's leading astray.

> What is the intent: a class where each instance is aware of every other instance - yet
> the word "Singleton" implies there's only one (cf a dict full of ...)?

The latter.

Regards,
Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B


More information about the Python-list mailing list