Consider:
class foo:
x = 5
def getx(r):
a = foo()
if r != 0:
a.x = r
return a
a = getx(8)
------------------------------------------------
the above works (unless I typoed it, I'm tired)
this illustrates instatiation of a class with initialization dependent
upon a passed parameter. Seems quite useful in the general sense (in fact,
I have a use for it, which is what led me to this.) But this doesn't work:
class foo(xstart):
x = 5
if xstart != 0:
x = xstart
a = foo(8)
What I am curious about is why not? What am I missing about classes here?
Is the functionality delivered in some other fashion, or must I:
class foo:
x = 5
a = foo()
a.x = 8
to get the brevity and clarity of...
class foo(xstart):
x = 5
if xstart != 0:
x = xstart
a = foo(8)
Thanks for any input on this. 4 1163
Ben wrote: class foo(xstart): x = 5 if xstart != 0: x = xstart
a = foo(8)
What I am curious about is why not? What am I missing about classes here? Is the functionality delivered in some other fashion, or must I:
class foo: x = 5
a = foo() a.x = 8
The parentheses after a class name do not indicate a parameter list;
they indicate the list of base classes. So generally, they must by
classes/types: def make_class(base):
.... class C(base):
.... pass
.... return C
.... make_class(object)
<class '__main__.C'> make_class(1)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in make_class
TypeError: Error when calling the metaclass bases
int() takes at most 2 arguments (3 given)
If you want to set a class or instance variable for an object at
instantiation time, you probably want to supply the __init__ method:
class C(object):
.... x = 5
.... def __init__(self, x):
.... if x != 0:
.... C.x = x
.... c = C(0) c.x
5 c = C(8) c.x
8
It seems strange to me that you want to set a *class* variable here, not
an instance variable, though perhaps you have your reasons. Note that
by doing so, every time you make a new instance, you'll change the x
attribute for all objects:
C.x
5 c1 = C(0) c1.x
5 C.x
5 c2 = C(8) c2.x
8 c1.x
8 C.x
8
If instead, you intended to set an instance variable, you might write it
like:
class C(object):
.... def __init__(self, x):
.... if x != 0:
.... self.x = x
.... else:
.... self.x = 5
.... c = C(0) c.x
5 c = C(8) c.x
8
Steve
Steven Bethard wrote: Ben wrote:
class foo(xstart): x = 5 if xstart != 0: x = xstart
a = foo(8)
What I am curious about is why not? What am I missing about classes here? Is the functionality delivered in some other fashion, or must I:
class foo: x = 5
a = foo() a.x = 8
The parentheses after a class name do not indicate a parameter list; they indicate the list of base classes. So generally, they must by classes/types:
>>> def make_class(base): .... class C(base): .... pass .... return C .... >>> make_class(object) <class '__main__.C'> >>> make_class(1) Traceback (most recent call last): File "<interactive input>", line 1, in ? File "<interactive input>", line 2, in make_class TypeError: Error when calling the metaclass bases int() takes at most 2 arguments (3 given)
If you want to set a class or instance variable for an object at instantiation time, you probably want to supply the __init__ method: >>> class C(object): .... x = 5 .... def __init__(self, x): .... if x != 0: .... C.x = x .... >>> c = C(0) >>> c.x 5 >>> c = C(8) >>> c.x 8
It seems strange to me that you want to set a *class* variable here, not an instance variable, though perhaps you have your reasons. Note that by doing so, every time you make a new instance, you'll change the x attribute for all objects: >>> C.x 5 >>> c1 = C(0) >>> c1.x 5 >>> C.x 5 >>> c2 = C(8) >>> c2.x 8 >>> c1.x 8 >>> C.x 8
If instead, you intended to set an instance variable, you might write it like: >>> class C(object): .... def __init__(self, x): .... if x != 0: .... self.x = x .... else: .... self.x = 5 .... >>> c = C(0) >>> c.x 5 >>> c = C(8) >>> c.x
8
Steve
Don't forget, though, that due to the attribute resolution order, if an
instance doesn't have a particular attribute but the class does then a
reference to the attribute in a method will get the class attribute.
This has been used to implement class defaults in the past.
regards
another Steve
-- http://www.holdenweb.com http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
On Mon, 22 Nov 2004 06:39:53 +0000, Steven Bethard wrote: If instead, you intended to set an instance variable, you might write it like:
>>> class C(object): ... def __init__(self, x): ... if x != 0: ... self.x = x ... else: ... self.x = 5 ... >>> c = C(0) >>> c.x 5 >>> c = C(8) >>> c.x
8
Steve,
That's exactly what I wanted. Thank you very much. I'll keep the class one
in mind too.
--Ben
--Ben
Ben wrote: On Mon, 22 Nov 2004 06:39:53 +0000, Steven Bethard wrote:
>>> class C(object): ... def __init__(self, x): ... if x != 0: ... self.x = x ... else: ... self.x = 5 ... >>> c = C(0) >>> c.x 5 >>> c = C(8) >>> c.x 8
Steve,
That's exactly what I wanted. Thank you very much. I'll keep the class one in mind too.
You're welcome. Depending on your goal here, you might also find that
default argument values are helpful: class C(object):
.... def __init__(self, x=5):
.... self.x = x
.... c = C() c.x
5 c = C(8) c.x
8 c = C(0) c.x
0
Steve This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hal Vaughan |
last post by:
I think a lot of this is definately a question of personal programming
style, but I'm new to Java and would like to hear a few opinions.
I'm writing a control panel for an application that runs...
|
by: Gonçalo Rodrigues |
last post by:
Hi,
For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to...
|
by: Jon Slaughter |
last post by:
I have a class that is basicaly duplicated throughout several files with
only members names changing according to the class name yet with virtually
the exact same coding going on.
e.g.
class...
|
by: mlimber |
last post by:
This is a repost (with slight modifications) from
comp.lang.c++.moderated in an effort to get some response.
I am using Loki's Factory as presented in _Modern C++ Design_ for
message passing in...
|
by: Random |
last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I
understand the purpose is so the developer can put classes within namespaces
to essentially organize your code. And I...
|
by: rodchar |
last post by:
Hey all,
I'm trying to understand Master/Detail concepts in VB.NET. If I do a data
adapter fill for both customer and orders from Northwind where should that
dataset live?
What client is...
|
by: jojobar |
last post by:
Hello,
I am trying to use vs.net 2005 to migrate a project originally in vs.net
2003. I started with creation of a "web site", and then created folders for
each component of the site.
I read...
|
by: Peter Oliphant |
last post by:
I just discovered that the ImageList class can't be inherited. Why? What
could go wrong? I can invision a case where someone would like to add, say,
an ID field to an ImageList, possible so that...
|
by: tshad |
last post by:
I am playing with Inheritance and want to make sure I understand it.
I have the following Classes:
*******************************************
Public Class AuthHeader:Inherits SoapHeader
Public...
|
by: heddy |
last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
| |