1,105,290 Community Members

classes in python help

Member Avatar
LdaXy
Light Poster
32 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
snippsat
Veteran Poster
1,039 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 10 [?]
 
0
 

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'
>>>
Member Avatar
LdaXy
Light Poster
32 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 2 Years Ago by snippsat
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: