We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,145 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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?

2
Contributors
2
Replies
55 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
LdaXy
Light Poster
32 posts since Dec 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 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'
>>>
snippsat
Posting Shark
956 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

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
Skill Endorsements: 0
Question Answered as of 1 Year Ago by snippsat

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0859 seconds using 2.7MB