473,399 Members | 3,401 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,399 software developers and data experts.

mutable member, bug or ...

By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near the end ).
class Cgroup_info:
group_name = ""
count = "0" #last time checked and processed/retrieved
first = "0"
last = ""
retrieval_type = "" # allways , ask( if more than some limit), none
date_checked = ""
time_checked = ""
new_count = ""
new_first = ""
new_last = ""
# local storage maintanance vars
pointer_file = ""
message_file = ""
#maintanance vars
cur_mess_num = 0
cur_mess_id = ""

def __init__( self ):
group_name = ""
count = "0" #last time checked and processed/retrieved

def get_count( self ):
print self.count, type( self.count )
return string.atoi( self.count, 10 )

class server_info:
def "(server_info::)"get_group_stat( self, grp ):

gr_info = Cgroup_info()
gr_info.group_name = grp
try:
ind = self.group_list.index( grp )
except ValueError:
gr_info.count(0)
return ( gr_info )
print ind
if len( self.group_list[ind].split() ) == 4:
gr_info.count = self.group_list[ind].split()[1]
gr_info.first = self.group_list[ind].split()[2]
gr_info.last = self.group_list[ind].split()[3]
else:
gr_info.count = gr_info.first = gr_info.last = "0"
return( gr_info )
Jun 4 '06 #1
3 1435
Sambo wrote:
By accident I assigned int to a class member 'count' which was
initialized to (empty) string and had no error till I tried to
use it as string, obviously. Why was there no error on assignment
(near the end ).


Python uses dynamic typing, which means that objects have types, but
variables don't. A variable is just a name.

I suggest reading up on variables, namespaces and classes/instances in
your favourite tutorial. Python's not C++, and you won't get very far
by pretending that it is.

</F>

Jun 4 '06 #2
Sambo a écrit :
By accident I assigned int to a class member 'count' which was
initialized to (empty) string and had no error till I tried to use it as
string, obviously. Why was there no error on assignment( near the end ).
Python is dynamically typed - which means that it's not the name that
holds type info, but the object itself. names are just, well, names...

BTW, I failed to see where you assigned an int to the class attribute
'count'. I just saw a try to call a string - which should raise a TypeError.
class Cgroup_info:
Do yourself (and anyone having to work on or with your code) a favour:
use new-style classes (ie : inherit from 'object'). And FWIW, the
convention for class names is CamelCase - preferably without MS-like
hungarian annotation.
group_name = ""
count = "0" #last time checked and processed/retrieved
first = "0"
last = ""
retrieval_type = "" # allways , ask( if more than some limit),
none
date_checked = ""
time_checked = ""
new_count = ""
new_first = ""
new_last = ""
# local storage maintanance vars
pointer_file = ""
message_file = ""
#maintanance vars cur_mess_num = 0
cur_mess_id = ""
All these are *class* attributes (meaning they belong to the class
object itself, not to instances). I really doubt this is what you want.
If you hope to have all the above as instance attributes, you must
assign them to the instance (usually in the __init__() method, which
main purpose is to initialize the newly created instance.
def __init__( self ):
group_name = ""
this creates a local variable 'group_name', bound to an empty string.
Using the reference to the current instance (usually named 'self', and
always passed in as first param) is *not* optional.
count = "0" #last time checked and processed/retrieved
and this creates a local variable 'count', bound to string '0'.

Of course, both are lost as soon as the __init__() returns.
def get_count( self ):
print self.count, type( self.count )
return string.atoi( self.count, 10 )
the string module is mostly deprecated. Use str object methods instead -
or if you just want to create an int from it's representation as a
string, int(self.count).
class server_info:
def "(server_info::)"get_group_stat( self, grp ):
invalid syntax.
gr_info = Cgroup_info()
and a problem with indentation here.
gr_info.group_name = grp
Tthis create a new instance attribute "group_name" on the gr_info
object. This instance attribute will shadow the class attribute of the
same name.

Also, FWIW, if you always know the value for group_name when
instanciating a Cgroup_info object, you might as well pass it to the
initializer.
try:
ind = self.group_list.index( grp )
The common convention for indices in each and every language is 'i'. If
you really want a meaningful name, then group_index would be better.

Also, for this kind of lookups, dicts are really faster than lists.
except ValueError:
gr_info.count(0)
I didn't see any method "count()" in the declaration of class
CGroup_info. I saw an attribute named "count", but it was bound to a
string - which is not callable.
return ( gr_info )
parenthesis here are useless (and FWIW, they would be just as useless in
C++).
print ind
if len( self.group_list[ind].split() ) == 4:
gr_info.count = self.group_list[ind].split()[1]
gr_info.first = self.group_list[ind].split()[2]
gr_info.last = self.group_list[ind].split()[3]
group_list[ind] is the same as grp, isn't it ? if so, using grp directly
might be much more efficient *and* much more readable.

Also, you're calling 4 times the same method. This is highly
inefficient. Try this instead:
parts = grp.split()
if len(parts) == 4:
gr_info.count, gr_info.first, gr_info.last = parts[1:]

else:
gr_info.count = gr_info.first = gr_info.last = "0"
This style of "chained assignment" can be a real gotcha in Python. In
this case, it is safe since "0" is immutable. But using a mutable object
instead would lead to probably unexpected results. Try this:

a = b = []
a.append(1)
print b

You have to understand that Python "variables" (the correct name is
"bindings") are just the association (in a given namespace) of a name
and a *reference* (kind of a smart pointer) to an object.
return( gr_info )


Here's a possible rewrite of your code. It's certainly not how it should
be done, but (apart from going into wild guesses) it's difficult to come
up with anything better without knowing the specs. Anyway, it should
help you grasp a more pythonic way to do things:

class GroupInfo(object):

def __init__(self, group_name, count="0", first="0", last=""):
self.group_name = group_name
self.count = count #last time checked and processed/retrieved
self.first = first
self.last = last

self.retrieval_type = "" # always , ask( if more than some
limit), none
self.date_checked = ""
self.time_checked = ""
self.new_count = ""
self.new_first = ""
self.new_last = ""

# local storage maintanance vars
self.pointer_file = ""
self.message_file = ""

#maintanance vars
self.cur_mess_num = 0
self.cur_mess_id = ""

# if you want to store count as string
# but retrieve it as an int. The setter will
# be used by the initializer, and will then create
# the implementation attribute _count
def _get_count(self):
return int(self._count)

def _set_count(self, value):
self._count = str(value)

count = property(_get_count, _set_count)
class ServerInfo(object):
def __init__(self, ???)
self._groups = {} # dict lookup is faster
# ...
def has_group(self, group_name):
return self._groups.has_key(group_name)

def get_group_stat(self, group_name):
if not self.has_group(group_name):
return GroupInfo(group_name, "0")

parts = group_name.split()
if len(parts) != 4:
parts = [None, "0", "0", "0"]
return GroupInfo(group_name, *(parts[1:4]))
As a last point, I'd second Fredrik : don't try to write C++ in Python.
Learn to write Python instead. The (freely available) "Dive into Python"
book should be a good place to get started.

HTH
Jun 4 '06 #3
Bruno Desthuilliers wrote:
Sambo a écrit :
By accident I assigned int to a class member 'count' which was
initialized to (empty) string and had no error till I tried to use it
as string, obviously. Why was there no error on assignment( near the
end ).

Python is dynamically typed - which means that it's not the name that
holds type info, but the object itself. names are just, well, names...

Hmm.. so I could have one instance with (int)count and (string)count?
(yay DBase flashback, BRRR)
I was going to initialize the vars in __init__() but somehow it didn't make sense to me( but left couple of them there).

BTW, I failed to see where you assigned an int to the class attribute
'count'. I just saw a try to call a string - which should raise a
TypeError.
I must have fixed it before posting.
gr_info.count = gr_info.first = gr_info.last = 0

??? except ValueError:
gr_info.count(0) ???
not sure what I was thinking there (maybe I was trying to triple fault the CPU hehe)
class Cgroup_info:

Do yourself (and anyone having to work on or with your code) a favour:
use new-style classes (ie : inherit from 'object'). And FWIW, the
convention for class names is CamelCase - preferably without MS-like
hungarian annotation.

Those damn hungarians with their calculators and notations, only later did it occur to me to paste the "class ServerInfo():" statement above.
this creates a local variable 'group_name', bound to an empty string.
Using the reference to the current instance (usually named 'self', and
always passed in as first param) is *not* optional.
count = "0" #last time checked and processed/retrieved
In __init__() it was an oversight but after the class className()...
I may have thought it unnecessary, otherwise class consists only of a group of functions if as you say any vars should be created/initialized in __init__() hmmm.
the string module is mostly deprecated. Use str object methods instead -
or if you just want to create an int from it's representation as a
string, int(self.count).

Are string object methods among others, things like:
words = sentence.split()
This no longer works for me is it because I imported 'string' ,
didn't import something or didn't use "from string import *" instead.
( must be a year since I last played with python.)

gr_info.group_name = grp

Tthis create a new instance attribute "group_name" on the gr_info
object. This instance attribute will shadow the class attribute of the
same name.

Hmmm . so what is a class attribute good for?
Also, FWIW, if you always know the value for group_name when
instanciating a Cgroup_info object, you might as well pass it to the
initializer.
Good point.
try:
ind = self.group_list.index( grp )

The common convention for indices in each and every language is 'i'. If
you really want a meaningful name, then group_index would be better.

lol, well like in a book and increasingly on the net index in used to refer to a list. So... I guess subscribed_group_list_index(_pointer) might be workable.
Also, for this kind of lookups, dicts are really faster than lists.
I am storing group count first last, although I am considering moving the numeric data elsewhere, for about 100 items .. I'll leave dictionaries for future learning.
return ( gr_info )

parenthesis here are useless (and FWIW, they would be just as useless in
C++).

A habit, true in python , in C, I think I remember reading about return function and statement. I was important at some point in C or perhaps way back in Pascal.

print ind
if len( self.group_list[ind].split() ) == 4:
gr_info.count = self.group_list[ind].split()[1]
gr_info.first = self.group_list[ind].split()[2]
gr_info.last = self.group_list[ind].split()[3]

group_list[ind] is the same as grp, isn't it ? if so, using grp directly
might be much more efficient *and* much more readable.

no grp is the (string) group name that was used earlier to find the right
item in the list. Also, you're calling 4 times the same method. This is highly
inefficient. Try this instead:
parts = grp.split()
if len(parts) == 4:
gr_info.count, gr_info.first, gr_info.last = parts[1:]

yes I realized that in another function but forgot about the unpacking assignment of a slice.
else:
gr_info.count = gr_info.first = gr_info.last = "0"

This style of "chained assignment" can be a real gotcha in Python. In
this case, it is safe since "0" is immutable. But using a mutable object
instead would lead to probably unexpected results. Try this:

a = b = []
a.append(1)
print b

I rarely do that even in C particularly when working with struct members,
but now with shorter names it is affordable. barely remembered reading about it.

You have to understand that Python "variables" (the correct name is
"bindings") are just the association (in a given namespace) of a name
and a *reference* (kind of a smart pointer) to an object.
return( gr_info )

Here's a possible rewrite of your code. It's certainly not how it should
be done, but (apart from going into wild guesses) it's difficult to come
up with anything better without knowing the specs. Anyway, it should
help you grasp a more pythonic way to do things:

class GroupInfo(object):

def __init__(self, group_name, count="0", first="0", last=""):
self.group_name = group_name
self.count = count #last time checked and processed/retrieved
self.first = first
self.last = last

self.retrieval_type = "" # always , ask( if more than some
limit), none
self.date_checked = ""
self.time_checked = ""
self.new_count = ""
self.new_first = ""
self.new_last = ""

# local storage maintanance vars
self.pointer_file = ""
self.message_file = ""

#maintanance vars
self.cur_mess_num = 0
self.cur_mess_id = ""

# if you want to store count as string
# but retrieve it as an int. The setter will
# be used by the initializer, and will then create
# the implementation attribute _count
def _get_count(self):
return int(self._count)

def _set_count(self, value):
self._count = str(value)

count = property(_get_count, _set_count)
class ServerInfo(object):
def __init__(self, ???)
self._groups = {} # dict lookup is faster
# ...
def has_group(self, group_name):
return self._groups.has_key(group_name)

def get_group_stat(self, group_name):
if not self.has_group(group_name):
return GroupInfo(group_name, "0")

parts = group_name.split()
if len(parts) != 4:
parts = [None, "0", "0", "0"]
return GroupInfo(group_name, *(parts[1:4]))
As a last point, I'd second Fredrik : don't try to write C++ in Python.
Learn to write Python instead. The (freely available) "Dive into Python"
book should be a good place to get started.

HTH


New class style eh, I haven't got a grasp of the old style and exceptions
only out of necessity. Either the Python exception docs were easier to understand or simply since I had to, to use those Inet classes , I finally broke down. I may have to go to C with this, for the GUI and I don't think building list of 100,000 messages is realistic .
Well Thank You for all the good pointers.
Jun 12 '06 #4

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

Similar topics

3
by: Thomas Matthews | last post by:
Hi, I understand that a member function can be declared as const {meaning it doesn't alter the member variables), but can it be declared as mutable? I have class that stores its members into...
5
by: Adam H. Peterson | last post by:
I'm using a private base class as part of the implementation for one of my classes. In my implementation, it holds cached data, and I'd like to be able to call its const member functions from my...
1
by: wtnt | last post by:
Hello. I previously had a program that compiled and worked with no error. Relevant parts here: class BasicList{ public: char* listLookup() { item = buffer; ...
2
by: pit3k | last post by:
How can I call non-const member function of a member of a class in one of it's const qualified member function so that a compiler won't complain? Here's the code: bool otl_stream::eof(); ...
3
by: Ingo Nolden | last post by:
Hi, I try to use const where ever appropriate. In a collection class I am counting the iterators that are out. The counter decrements when an iterator leaves scope or is 'Dispose( )'d While...
6
by: christopher diggins | last post by:
I wrote a dynamic matrix class similar to the one described in TCPL 3rd Edition. Rather than define two separate iterators for const and non-const scenarios I decided to be a lazy bastard and only...
5
by: Pelle Beckman | last post by:
Hi, Honestly, what is this 'size_t'? And while I'm at it - what is a 'mutable' var.? I've understood it somehow makes a const a non-const, but what's the point of that? Short explanations?...
18
by: Markus.Elfring | last post by:
The C++ language specification provides the key word "mutable" that is not available in the C99 standard. Will it be imported to reduce any incompatibilities?...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.