473,382 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Initializing a subclass with a super object?

Class A inherits from class B. Can anyone point me in the direction
of documentation saying how to initialize an object of A, a1, with an
object of B, b1?
Jun 27 '08 #1
4 1799
On May 11, 7:22 am, frankdmarti...@gmail.com wrote:
Class A inherits from class B. Can anyone point me in the direction
of documentation saying how to initialize an object of A, a1, with an
object of B, b1?
This is called a 'factory'in design patterns. Search 'python factory',
you will get a lot of examples.

br, Terry
Jun 27 '08 #2
Hi, Terry.
Yeah, no. If we think of the inherited B as an object nested
within a1, I'm attempting to initialize that B with b1 by accessing
the B, say, via a function call. I don't see how using a python
factory achieves this.
Jun 27 '08 #3
On Sat, 10 May 2008 18:09:02 -0700, frankdmartinez wrote:
Hi, Terry.
Yeah, no. If we think of the inherited B as an object nested
within a1, I'm attempting to initialize that B with b1 by accessing
the B, say, via a function call. I don't see how using a python
factory achieves this.
But there is not such a thing, in Python. What you have is that A
has the same attributes/methods of B plus its own.
What you could do is adding in class A a method like this:

class A(B):
...
def set_b_attributes(self, instance_of_b):
for k, value in instance_of_b.__dict__:
setattr(self, k, value )

and the use it like this:

a1.set_b_attributes(b1)

Of course, if b attributes are few and always the same it is more
readable assigning them explicitely:

def set_b_attributes(self, instance_of_b):
self.attr1 = instance_of_b.attr1
self.attr2 = instance_of_b.attr2
....

Ciao
-----
FB


Jun 27 '08 #4
On May 11, 3:19*am, Francesco Bochicchio <bock...@virgilio.itwrote:
But there is not such a thing, in Python. What you have is that A
has the same attributes/methods of B plus its own.
What you could do is *adding in class A a method like this:

* class A(B):
* * *...
* * *def set_b_attributes(self, instance_of_b):
* * * * *for k, value in instance_of_b.__dict__:
* * * * * * * * setattr(self, k, value )

and the use it like this:

* *a1.set_b_attributes(b1)
Hi, Francesco.
Thanx! That's actually exactly what I needed (though I didn't
know it).
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
0
by: flupke | last post by:
I have the following test code setup, trying to get the class name of a subclass in the super class. (Reason why i want this is described below) file class_name_start.py ========================...
4
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
13
by: Ikke | last post by:
Hi everybody, Suppose I have a class 'Dog' defined as follows: public class Dog : Animal Now suppose I have the following code: Dog fido = null; Animal animal = new Animal(); Then how do...
1
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class...
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
5
by: pixiedustjt | last post by:
Hello, This is my first time to post on the forum, so I hope I'm doing this correctly. I am working on the dreading Java Inventory Program that seems to be legendary. I am currently on part 3...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.