Bottom Page

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,(),{})?
Quote
#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
Quote
#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.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Type hinting - return type based on parameter micseydel 2 233 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Animate graph nodes inside a function adamG 0 382 Sep-23-2019, 11:18 AM
Last Post: adamG
  Getting type from input() function in Python 3.0 leodavinci1990 7 651 Jul-29-2019, 08:28 PM
Last Post: avorane
  Conditional importing inside a function Roger 1 1,113 Jul-03-2019, 10:02 PM
Last Post: Gribouillis
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 787 May-11-2019, 03:18 AM
Last Post: keames
  function 2 inside function 1 parameters SheeppOSU 4 585 Apr-20-2019, 09:34 PM
Last Post: snippsat
  Type function does not work sunnyarora 2 545 Mar-15-2019, 10:50 AM
Last Post: sunnyarora
  Should a function ever be more permissive than its type hints? Shay 1 349 Mar-13-2019, 05:36 PM
Last Post: Larz60+
  I need help with loop one function and creating variables don 3 666 Jan-24-2019, 07:31 AM
Last Post: buran
  How can I get values inside of function? Mysth 4 588 Dec-31-2018, 03:52 PM
Last Post: Axel_Erfurt

Forum Jump:


Users browsing this thread: 1 Guest(s)