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

Question about classes

Ben
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.

Jul 18 '05 #1
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
Jul 18 '05 #2
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
Jul 18 '05 #3
Ben
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

Jul 18 '05 #4
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
Jul 18 '05 #5

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

Similar topics

5
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...
6
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...
21
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...
12
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...
11
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...
6
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...
10
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...
6
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...
6
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
jinu1996
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...
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.