954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

classes in python help

i'm learning python currently, coming from c++ and i realized that i can't do certain things like in C++

anyways, i created a class call Object_refs:

#attempt to make a structure like class using INIT
#to define all common variables.
class Object_refs:
	def __INIT__(self, descriptor_type, type, reference_count, flags):
		self.desctype = descriptor_type
		self.type = type
		self.refcount = reference_count
		self.flags = flags


now, i can do this:

OBJREF = Object_refs #i'm tying to define the class as another variable, like a typedef in c++


in the python terminal it shows:

>> OBJREF
<class __MAIN__.Object_refs at 0x0000000001E3F888>
>>


however, i cannot do this:

>> OBJREF.type = '\u01'
AttributeError: Object_refs has no attribute type


maybe i'm not understanding python classes correctly. could somebody help me try to understand this better?

LdaXy
Light Poster
32 posts since Dec 2011
Reputation Points: 10
Solved Threads: 2
 

You can look at this,and type is a reserved word in python.

class Object_refs(object):
    def __init__(self, descriptor_type, my_type, reference_count, flags=None):
        '''initializer method can be compared with constructor C++,but are not the same'''
        self.desctype = descriptor_type
        self.my_type = my_type
        self.refcount = reference_count
        self.flags = flags

Use class.

>>> descriptor_type = 1
>>> my_type = 'string'
>>> reference_count = 10
>>> obj = Object_refs(descriptor_type,my_type,reference_count)
>>> obj.my_type
'string'
>>> obj.my_type = '\u01'
>>> obj.my_type
'\\u01'
>>>
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

oh. had no idea type was reserved. thanks for the info. this makes things much clearer.

LdaXy
Light Poster
32 posts since Dec 2011
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: