Python Forum
Creating function inside classes using type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating function inside classes using type
#1
I am am stuck, i want to define a class with __init__ hsing the type format

Class A:
  phish = 'Band'
a = A()

# above is the same as

a = type(A,(),{'phish':'Band'}
But, what is the equivalent type() syntax for:

Class A:
   def __init__(self)
      self.phish='Band'
      
a = A()

a = type(A,(),{'__init__':*something here*}
when i print attr.items() i get something like '__init__', <function A.__init__ at 0x0000015D216FB9D8> in the first example

how could i get a similar result in a second example


Complete question: How do i define class functions using type(cls,(),{})?
Reply
#2
have you tried to run this?
Class A: will cause a syntax error
class has no title case.
also closing parenthesis on lines 7 (both of them) are missing
Reply
#3
You can define unbound method __init__, e.g.

def unbound_init(s):
    s.phish = 'Band'

# and create class instance (and the class) dynamically
a_instance = type('A', (), {'__init__': unbound_init})()

# or maybe you wish one-liner,

a_instance = type('A', (), {'__init__': lambda self: setattr(self, 'phish', 'Band1')})()
Now, a_instance.phish is Band1.
Reply
#4
(Mar-19-2020, 10:08 PM)Larz60+ Wrote: have you tried to run this?
Class A: will cause a syntax error
class has no title case.
also closing parenthesis on lines 7 (both of them) are missing

No i did not try to run it, thought it sufficed to get my point accross.

(Mar-19-2020, 10:15 PM)scidam Wrote: You can define unbound method __init__, e.g.

def unbound_init(s):
    s.phish = 'Band'

# and create class instance (and the class) dynamically
a_instance = type('A', (), {'__init__': unbound_init})()

# or maybe you wish one-liner,

a_instance = type('A', (), {'__init__': lambda self: setattr(self, 'phish', 'Band1')})()
Now, a_instance.phish is Band1.


Thanks! This is going to be super useful <3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  determine parameter type in definition function akbarza 1 550 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  with open context inside of a recursive function billykid999 1 549 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 752 May-02-2023, 08:40 AM
Last Post: Gribouillis
  i want to use type= as a function/method keyword argument Skaperen 9 1,771 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  match type with value in csv parsing function vinci 2 1,268 Jan-21-2022, 12:19 PM
Last Post: Larz60+
  How to make global list inside function CHANKC 6 2,978 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Parameters aren't seen inside function Sancho_Pansa 8 2,814 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  Creating a variables inside FOR loop zazas321 5 4,038 Sep-16-2020, 04:42 PM
Last Post: Naheed
  [split] Creating a variable as a function DPaul 23 6,508 Sep-07-2020, 05:20 PM
Last Post: DPaul
  Creating a variable as a function JarredAwesome 4 2,781 Sep-06-2020, 05:08 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020