I have the following Python class with a class counter and self-referential assignment as follows:
class Foo(object):
counter = 0 # a class attributeinitialized to 0 that is shared across all instances of the class Foo
def __init__(self, bar=True, dict=None):
self._bar = bar # instance attribute `_bar` gets `bar`
self._baz = None # instance attribute `_baz` gets `None`
if bar:
self.dict = {self: 1} # self-referential assignment
self.counter = Foo.counter # instance attribute `counter` gets current value of the class attribute `counter`
Foo.counter += 1 # increase class attribute `counter` by 1, when bar is `True`
else:
self.dict = dict # instance attribute `dict` gets `dict`
self.counter = None # instance attribute `counter` gets `None` when bar is `False`
Sample instances would be
foo1 = Foo()
print(f'foo1._bar: {foo1._bar}, foo1._baz: {foo1._baz}, foo1.dict: {foo1.dict}, foo1.counter: {foo1.counter}')
# output
# foo1._bar: True, foo1._baz: None, foo1.dict: {<__main__.Foo object at 0x000002A50597E210>: 1}, foo1.counter: 0
custom_dict = {'a': 1, 'b': 2}
foo2 = Foo(bar=False, dict=custom_dict)
print(f'foo2._bar: {foo2._bar}, foo2._baz: {foo2._baz}, foo2.dict: {foo2.dict}, foo2.counter: {foo2.counter}')
# output
# foo2._bar: False, foo2._baz: None, foo2.dict: {'a': 1, 'b': 2}, foo2.counter: None
foo3 = Foo()
print(f'foo3._bar: {foo3._bar}, foo3._baz: {foo3._baz}, foo3.dict: {foo3.dict}, foo3.counter: {foo3.counter}')
# output
# foo3._bar: True, foo3._baz: None, foo3.dict: {<__main__.Foo object at 0x000002A50597F7D0>: 1}, foo3.counter: 1
In Julia, I am trying to create a mutable struct called Foo
that will be equivalent to the Python class Foo
. In particular for my purpose it is better if I can instantiate similarly to the Python code, e.g., foo 1 = Foo()
, foo2 = Foo(bar=False, dict=custom_dict)
and so on. I will appreciate any tips or suggestions regarding how to do that.