Class that takes another class as argument, copies behavior

2024/10/14 15:30:40

I'd like to create a class in Python that takes a single argument in the constructor, another Python class. The instance of the Copy class should have all the attributes and methods of the original class, without knowing what they should be beforehand. Here's some code that almost works:

import copyclass A():l = 'a'class Copy():def __init__(self, original_class):self = copy.deepcopy(original_class)print(self.l)c = Copy(A)
print(c.l)

The print statement in the constructor prints 'a', but the final one gives the error AttributeError: Copy instance has no attribute 'l'.

Answer

You need to copy the __dict__:

import copyclass A():l = 'a'class Copy():def __init__(self, original_class):self.__dict__ = copy.deepcopy(original_class.__dict__)print(self.l)c = Copy(A)  # -> a
print(c.l)  # -> a
https://en.xdnf.cn/q/117941.html

Related Q&A

Simple python script to get a libreoffice base field and play on vlc

Ive banged my head for hours on this one, and I dont understand the LibreOffice macro api well enough to know how to make this work:1) This script works in python:#!/usr/bin/env python3 import subproce…

Print month using the month and day

I need to print month using the month and day. But I cannot seem to move the numbers after 1 to the next line using Python.# This program shows example of "November" as month and "Sunday…

Maya: Defer a script until after VRay is registered?

Im trying to delay a part of my pipeline tool (which runs during the startup of Maya) to run after VRay has been registered. Im currently delaying the initialization of the tool in a userSetup.py like…

Optimization on Python list comprehension

[getattr(x, contact_field_map[communication_type])for x in curr_role_group.contacts ifgetattr(x, contact_field_map[communication_type])]The above is my list comprehension. The initial function and the …

tensorflow Word2Vec error

I downloaded source code of word2vec in github below. https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec.py I am using tensorflow on pycharm. Im using windows 10. I installed…

Mysterious characters at the end of E-Mail, received with socket in python

I am not sure if this is the right forum to ask, but I give it a try. A device is sending an E-Mail to my code in which I am trying to receive the email via a socket in python, and to decode the E-Mail…

Error when importingPython-vlc

I have installed python-vlc D:\Programing\Python\Python 3.6>python -m pip install python-vlc Requirement already satisfied: python-vlc in d:\programing\python\python 3.6\lib\site-packages\python_vlc…

How to use FEniCS in Jupyter Notebook or Spyder?

I have recently installed FEniCS using Docker with following commandTerminal> curl -s https://get.fenicsproject.org | bashEvery thing works well when I am in fenicsproject session. In this session w…

Error installing polyglot in python 3.5.2

I want to do sentiment analysis on urdu sentences. I searched a python package Polyglot having URDU POS tagger in it. But on installing, it prompts error;Any way out?

How do you turn a dict of lists into a list of dicts with all combinations?

Basically what I am looking for is the equivalent of itertools.product but for dicts.For example, say I want to test a function for all combinations of its keyword arguments, I would like to pass lists…