473,395 Members | 1,368 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,395 software developers and data experts.

How to declare a Class inside another class as avariable?

440 256MB
Hi,

How to declare a Class inside another class as avariable?

For Example"

Class X{

MethodX1();

}

Class Y{

Class X _xObj; # Declaration of class object as a variable in another class

MethodY1(){

_xObj = ..

_xObj ->MethodX1()

}

}
Feb 27 '07 #1
4 11715
bartonc
6,596 Expert 4TB
Hi,

How to declare a Class inside another class as avariable?

For Example"

Class X{

MethodX1();

}

Class Y{

Class X _xObj; # Declaration of class object as a variable in another class

MethodY1(){

_xObj = ..

_xObj ->MethodX1()

}

}
You can instantiate a class in Python the same way:
Expand|Select|Wrap|Line Numbers
  1. class MyClass(object):
  2.    # class and instance stuff here
  3.     def DoStuff(self):
  4.         print "hello", self
  5.  
  6. class InstantiatesMyClass(object):
  7.     def __init__(self):
  8.         self.myClassObj = MyClass()
  9. imc = InstantiatesMyClass()
  10. imc.myClassObj.DoStuff()
Feb 28 '07 #2
psbasha
440 256MB
You can instantiate a class in Python the same way:
Expand|Select|Wrap|Line Numbers
  1. class MyClass(object):
  2.    # class and instance stuff here
  3.     def DoStuff(self):
  4.         print "hello", self
  5.  
  6. class InstantiatesMyClass(object):
  7.     def __init__(self):
  8.         self.myClassObj = MyClass()
  9. imc = InstantiatesMyClass()
  10. imc.myClassObj.DoStuff()
Hi,

Thanks for the example.

So base classes has to be derived from the "object" python class.Is my undrestanding correct?.
Feb 28 '07 #3
Hi,

Thanks for the example.

So base classes has to be derived from the "object" python class.Is my undrestanding correct?.
Remember that when you use classes in python it would look something like this:
Expand|Select|Wrap|Line Numbers
  1. class myFrame(wx.Frame):
  2.          def __init__(self,parent,id,title):
  3.                     wx.Frame.__init__(self,parent=None,id,title)
  4.          def somethingelse(self):
  5.                     Dosomethingelse;
  6.  
  7.  
Where 'myFrame' is the name of your class.
And you inherit methods from 'wx.Frame'
So you can use the __init__ method from the 'wx.Frame' class
As previously mentioned, all you have to do is to create an instance of your class to use it inside another class for example

Expand|Select|Wrap|Line Numbers
  1.  
  2. class newclass:
  3.          def __init__(self):
  4.                  instance = myFrame(None,-1,"Just some parameters")
  5.  
  6.  
Does this answer your question?
Mar 1 '07 #4
bartonc
6,596 Expert 4TB
Hi,

Thanks for the example.

So base classes has to be derived from the "object" python class.Is my undrestanding correct?.
Only for "New Style" inheritance to work. It is the recommended coding since about version 2.4.
Mar 2 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mosfet | last post by:
Hi, I have some problems to write my class : if I put the structure declaration before my class i have a compiler error since my CHTTPServerCe is not yet known from the compiler but if I put...
2
by: John Black | last post by:
Hi, Suppose I have a class, class MyClass{ public: MyClass(); MyClass(MyClass); int func(); };
4
by: Mark | last post by:
Hi, is it possible to declare a delegate inside a routine? E.g private void MySub() { delegate int oAddMe(int n1, int n2); } Or can delegates only be declared with a global scope inside...
6
by: The8thSense | last post by:
how to declare static varible and static method inside a class ? i try "public static ABC as integer = 10" and it said my declaration is invalid Thanks
2
by: Warex | last post by:
Hello, I have 2 questions maybe someone can answer. 1. On a function class when you return an item is it possible to return more than one and how is that done? Currently I am using for one...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
4
by: =?Utf-8?B?UmljaGFyZEBub3NwYW0ubm9zcGFt?= | last post by:
I'm trying to declare CreateFile as an extern from the dll like so: .... using System.Runtime.InteropServices; internal static extern IntPtr CreateFile( String filename, UInt32...
2
by: yogeshbhandare | last post by:
Is there any special reason that we cannot define a class inside a function We can instantiate a class inside a function but not define ? What could be the cause of such a behavior? We can define...
2
by: Tony Johansson | last post by:
Hello! Below I have two different alternativ for where to declare the delegate DoCalculate. Is it possible to say something generally where to declare the delegate. Is best within the class or...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.