Re: Trailer for upcoming Python documentary

2025-05-19 Thread Christian Buhtz via Python-list

Am 18.05.2025 22:16 schrieb Larry Martell via Python-list:

https://youtu.be/pqBqdNIPrbo?si=P2ukSXnDj3qy3HBJ


Awesome! Which release channels will be used? How can we pay?
--
https://mail.python.org/mailman/listinfo/python-list


Dynamic classes

2025-05-19 Thread Jonathan Gossage via Python-list
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


Re: Dynamic classes

2025-05-19 Thread Mats Wichmann via Python-list

On 5/19/25 09:51, Jonathan Gossage via Python-list wrote:

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.


Might help if you show the init function. I've done something similar to 
this without trouble, but not using the unpacking (i.e. *args). I used 
this in an ancient blog post (thus, pre-typing, and such):


def transact(acct, amount):
acct.balance += amount

def pay_interest(acct):
acct.balance += acct.balance * acct.interest_rate

def account_init(acct, num, name, bal, rate):
acct.acct_number = num
acct.acct_holder = name
acct.balance = bal
acct.interest_rate = rate

account = {
"acct_number": "XXX",
"acct_holder": "",
"balance": 0.0,
"interest_rate": 0.0,
"transact": transact,
"pay_interest": pay_interest,
"__init__": account_init,
}

AccountType = type("AccountType", (), account)

myaccount = AccountType("1234567", "J. Q. Public", 20.0, 0.01)
print(myaccount.balance)
myaccount.transact(-10)
print(myaccount.balance)
myaccount.pay_interest()
print(myaccount.balance)

--
https://mail.python.org/mailman/listinfo/python-list


Re: Dynamic classes

2025-05-19 Thread Thomas Passin via Python-list

On 5/19/2025 5:49 PM, Mats Wichmann via Python-list wrote:

On 5/19/25 09:51, Jonathan Gossage via Python-list wrote:

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.


Might help if you show the init function. I've done something similar to 
this without trouble, but not using the unpacking (i.e. *args). I used 
this in an ancient blog post (thus, pre-typing, and such):


def transact(acct, amount):
     acct.balance += amount

def pay_interest(acct):
     acct.balance += acct.balance * acct.interest_rate

def account_init(acct, num, name, bal, rate):
     acct.acct_number = num
     acct.acct_holder = name
     acct.balance = bal
     acct.interest_rate = rate

account = {
     "acct_number": "XXX",
     "acct_holder": "",
     "balance": 0.0,
     "interest_rate": 0.0,
     "transact": transact,
     "pay_interest": pay_interest,
     "__init__": account_init,
}

AccountType = type("AccountType", (), account)

myaccount = AccountType("1234567", "J. Q. Public", 20.0, 0.01)
print(myaccount.balance)
myaccount.transact(-10)
print(myaccount.balance)
myaccount.pay_interest()
print(myaccount.balance)



It's interesting that in Jython there is a way to do something 
conceptually similar to turn a Jython class into a Java class.  Here's 
one of mine:


synchronized CoordinatorType getCoord() {
JythonObjectFactory factory = new JythonObjectFactory (
// Type class, Jython module name, class name
CoordinatorType.class, "Coordinator", "Coordinator");

// Java class
CoordinatorType coord = (CoordinatorType) factory.createObject();
return coord;
}

// Instantiate a Coordinator
// Error handling elided for clarity
CoordinatorType c;
c = getCoord();




--
https://mail.python.org/mailman/listinfo/python-list


Re: Dynamic classes

2025-05-19 Thread Rob Cliffe via Python-list



On 19/05/2025 23:11, Thomas Passin via Python-list wrote:

On 5/19/2025 5:49 PM, Mats Wichmann via Python-list wrote:

On 5/19/25 09:51, Jonathan Gossage via Python-list wrote:

I have created a dynamic class using the type() function:
x = type('MyFlags', (), {'Flag1': 1, 'Flag2': 2, 'Flag3: 4, ' 
'__init__' :

__init__})

This is not my area of expertise, but there is a misplaced quote before
    '__init__'
that should be after
    'Flags3
Correct this, and your example runs without error.
Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list


Re: Dynamic classes

2025-05-19 Thread Greg Ewing via Python-list

On 20/05/25 4:33 am, Stefan Ram wrote:

   So, the reason you're getting that
   TypeError is your __init__ function isn't actually hooked up
   right when you build your class with "type". You got to set up
   your init before you call "type", and then drop it into the
   class dictionary as a /function/ (not as a string).


That's what he did, or at least that's what he tried to do.
It turns out the misplaced quote was the entire problem -- by a
fluke, it didn't result in a syntax error, and ended up putting
the __init__ function into the dict under the name
'Flag3: 4, __init__'.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list