[ale] Anyone familiar with Python?

Alex Carver agcarver+ale at acarver.net
Wed Mar 9 02:23:32 EST 2016


On 2016-03-08 19:48, Pete Hardie wrote:
> I think the @classmethod decorator will do what you want - you could make
> bus initialization in a class method with guards to perform it only once
> (and on the single set of values), and then call the subclasses fo the
> reading methods
> 
> 

[snip]

Interesting.  I tried to look up that decorator and have so far come
away with a variety of answers of varying quality.  I'll have to dig
into it some more.

So far what I'm gleaning is that I should be able to do something like:

class MyClass(object):

	@classmethod
	def __init__(cls, args_here):
		do_bus_stuff_but_only_once
		do_other_init_stuff

	def other_base_class_thing(self, args_here):
		other_stuff

class MySubclass(MyClass):

	def __init__(self):
		super(MySubclass, self).__init__()

	def subclass_thing(self):
		stuff

	def subclass_other_thing(self)
		other_stuff

And as such when I make these calls:

foo = MySubclass(...)
bar = MySubclass(...)

then in theory bus_init is the identical instance in both foo and bar
(so therefore the bus init happens) but the rest are not?  Did I
understand correctly or have I completely botched classmethod decorators?


More information about the Ale mailing list