Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python anonymous object


class MicroMock(object):
  def __init__(self, **kwargs):
    self.__dict__.update(kwargs)
  def print_foo(x):
    print x.foo
    print_foo(MicroMock(foo=3))
Comment

create anonymous objects in Python

>>> obj = type('',(object,),{"foo": 1})()
>>> obj.foo
1
Comment

create anonymous objects in Python

#So brief, such Python! O.o
>>> Object = lambda **kwargs: type("Object", (), kwargs)

#Then you can use Object as a generic object constructor:
>>> person = Object(name = "Bernhard", gender = "male", age = 42)
>>> person.name
'Bernhard'
>>>
Comment

PREVIOUS NEXT
Code Example
Python :: python how do index all odd numbers in a list 
Python :: addition array numpy 
Python :: negate an int in python 
Python :: dataframe partition dataset based on column 
Python :: from django.urls import path 
Python :: call methods from within a class 
Python :: qtablewidget add row python 
Python :: how to make a operating system in python 
Python :: bst in python 
Python :: class views django slug 
Python :: printing in python 
Python :: python for loop inside list 
Python :: python seq 
Python :: if a or b in python 
Python :: manifest.in python 
Python :: python web server oneliner 
Python :: flask decorator causes views to be named the same thing 
Python :: python copy formula ghseets 
Python :: mad libs game prompt python 
Python :: python update pip windows 
Shell :: bitnami restart apache 
Shell :: uninstall k3s 
Shell :: install dateutil 
Shell :: upgrade pillow version 
Shell :: reinstall mysql 
Shell :: remove remote origin github 
Shell :: copy ssh key ubuntu 
Shell :: yarn clear cache 
Shell :: yarn install global 
Shell :: fork bomb 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =