Forward References
I am attempting to use forward references in my program and I am failing. This also does not work with the older way of putting the name of a class as a string. Here is some sample code: from __future__ import annotations from dataclasses import dataclass from typing import TypeAlias ColorDef: TypeAlias = RGB | int | str @dataclass(frozen=True, slots=True) class RGB(object): Can anyone suggest how I should fix this without reversing the statement order? pass -- Jonathan Gossage -- https://mail.python.org/mailman/listinfo/python-list
Using generator expressions
I am having a problem using generator expressions to supply the arguments for a class instance initialization. The following example shows the problem: class test1(object): def __init__(self, a, b): > self.name = a self.value = b st = 'Programming Renaissance, Any'.split(', ') y = test1(a for a in st) print(f'Object values are: {y._a}, {y._b}') I would expect to get the values from the list generated by splitting the string passed in as arguments to the new instance of test1, but instead I get the generator expression by itself as a generator object. The generator expression is treated like a passive object instead of being run. If I had wanted to pass the generator expression itself, I would have expected to have to use parentheses around the generator expression. Any suggestions on how to get the generator expression to run? If I change the definition of the input arguments to *args I can capture the arguments within __init__ but it is verbose and ugly. Also, I could accept the arguments from a Sequence and extract the Sequence members into the class values. I would prefer my solution if I could get it to work. Note that I tried generator expressions both inside parentheses and not, without success. -- Jonathan Gossage -- https://mail.python.org/mailman/listinfo/python-list
Re: Using generator expressions
Many thanks, all. It turned out that my problem was not fully understanding the use and power of the unpack operator *. Using it to activate my generator made things start to work. I changed the line where I invoked the generator to: y = test1(*(a for a in st)) adding the unpack operator. On Mon, Sep 25, 2023 at 11:15 AM Thomas Passin via Python-list < python-list@python.org> wrote: > On 9/25/2023 10:15 AM, Jonathan Gossage via Python-list wrote: > > I am having a problem using generator expressions to supply the arguments > > for a class instance initialization. The following example shows the > > problem: > > > > class test1(object): > > def __init__(self, a, b): > > > >> self.name = a > > > > self.value = b > > st = 'Programming Renaissance, Any'.split(', ') > > y = test1(a for a in st) > > print(f'Object values are: {y._a}, {y._b}') > > > > I would expect to get the values from the list generated by splitting the > > string passed in as arguments to the new instance of test1, but instead > > I get the generator expression by itself as a generator object. The > > generator > > expression is treated like a passive object instead of being run. If I > had > > wanted to pass the generator expression itself, I would have expected to > > have > > to use parentheses around the generator expression. Any suggestions on > how > > to > > get the generator expression to run? > > If I change the definition of the input arguments to *args I can capture > the > > arguments within __init__ but it is verbose and ugly. Also, I could > accept > > the > > arguments from a Sequence and extract the Sequence members into the class > > values. I would prefer my solution if I could get it to work. > > Note that I tried generator expressions both inside parentheses and not, > > without success. > > > > You should get an error at the y assignment. The argument of test1() is > a generator, which would get assigned to the "a" argument, and there > would be no "b" argument, which is an error. > > In any event, even if this were to work as you want, it would only work > for strings that contain one comma. And you ask for values like y._a, > but y._a is never created, only y.a. If you did convert the generator > to a list, and if you fix the underscored variable names, it still > wouldn't work because the arguments don't expect a list. > > Time to step back and figure out exactly what you actually want to do. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- Jonathan Gossage -- https://mail.python.org/mailman/listinfo/python-list
Using __new__
I am attempting to use the __new__ method in the following code: class SingletonExample(object): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls, **kwargs) return cls._instance def __init__(self, **kwargs) -> None: our_attributes = ('h', 'x') if kwargs is not None: for k, v in kwargs.items(): if k in our_attributes: setattr(self, k, v) a = SingletonExample(h=1) and I get the following result: (PRV) jonathan@jfgdev:/PR$ python -m Library.Testing.test2 Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 16, in a = SingletonExample(h=1) ^ File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 6, in __new__ cls._instance = super().__new__(cls, **kwargs) ^^ TypeError: object.__new__() takes exactly one argument (the type to instantiate) I am quite puzzled as it looks as if this code will not work if the super-class is 'object'. Any suggestions on how to proceed? -- Jonathan Gossage -- https://mail.python.org/mailman/listinfo/python-list
Pip installs to unexpected place
I am using *Python 3.13* in a virtual environment under *Ubuntu Linux 24.04* . The version of Python was compiled from source code and installed with make altinstall. I attempted to use *pip* to install the *Sphinx* package into the virtual environment using the command *pip install sphinx* in the virtual environment*.* I expected that *sphinx* would be installed in the *site-packages* directory in the virtual environment. Instead, it was installed into the site-packages directory in */home/jonathan/.locals/lib/python3.13/site-packages* even though I did not specify *--user* to the *pip install* command. Is this expected behavior? I wanted Sphinx to be installed in the virtual environment so that it would be accessible to all users of the virtual environment. -- Jonathan Gossage -- https://mail.python.org/mailman/listinfo/python-list
Dynamic classes
I have created a dynamic class using the type() function: x = type('MyFlags', (), {'Flag1': 1, 'Flag2': 2, 'Flag3: 4, ' '__init__' : __init__}) The new class is there, and the class variables, Flag1, Flag2, and Flag3, are present correctly. However, when I try to create an instance of this class with the following code: y = x('Flag1', 'Flag2') it fails with a TypeError stating that 'MyFlags' does not accept arguments. What do I have to do to make this happen?. BTW __init__(self, *args) is defined as the instance initializer. -- Jonathan Gossage -- https://mail.python.org/mailman/listinfo/python-list