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.
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.
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)
On 5/19/2025 5:49 PM, Mats Wichmann via Python-list wrote:This is not my area of expertise, but there is a misplaced quote before
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__})
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).
I have created a dynamic class using the type() function:
x = type('MyFlags', (), {'Flag1': 1, 'Flag2': 2, 'Flag3: 4, ' '__init__' : __init__})
I find that it's generally more convenient to do this using similar code:
def constructor(flag1, flag2):
class _Hidden:
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 9 |
Nodes: | 8 (0 / 8) |
Uptime: | 124:06:02 |
Calls: | 161 |
Files: | 21,502 |
Messages: | 78,954 |