By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

referencing an object attribute sort of indirectly from within list

P: n/a
Is there anything that would get in the way of me being able to reference an object attribute, from within a list.

I have:
# one class here

class BackupSet:
fileGroupList = []

# another class here

class FileGroup:
sourceDir = ''
destinDir = ''
def __init__(self):
self.sourceDir = 'c:\folder'
self.destinDir = 'd:\folder'
fileGroup = FileGroup()

backupSet = BackUpSet()

backupSet.fileGroupList.append(fileGroup)

# when i try to reference an attribute here, I just get 'none'

print bkset.fileGroupList[0].sourceDir or
print "%s" % bkset.fileGroupList[0].sourceDir


thanks
Steve
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 30 Nov 2003 01:32:05 GMT, "python newbie" <me*******@hotmail.com> wrote:
This is a multi-part message in MIME format. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- So far, so annoying
[...]
Is there anything that would get in the way of me being able to =
reference an object attribute, from within a list.

I have: Where do you have it? This is not an actual verbatim example of something you tried.
# one class here

class BackupSet: ^--- this is different from your constructor call below at [1] fileGroupList =3D []
=20
# another class here

class FileGroup:
sourceDir =3D ''
destinDir =3D ''
def __init__(self):
self.sourceDir =3D 'c:\folder'
self.destinDir =3D 'd:\folder' =20
fileGroup =3D FileGroup()

backupSet =3D BackUpSet() ^----- [1] So I know you didn't do this.
backupSet.fileGroupList.append(fileGroup)

# when i try to reference an attribute here, I just get 'none' Maybe you have some old code that reads
self.sourceDir = 'none'
in class FileGroup. Who can tell?

print bkset.fileGroupList[0].sourceDir or ^^^^^-- what the heck is that?
print "%s" % bkset.fileGroupList[0].sourceDir
=20

thanks
Steve
------=_NextPart_000_0025_01C3B69E.E2B92BA0
Content-Type: text/html;

<grr>
Two things:
1. Don't post HTML
2. Do post verbatim copy/pasted examples from the window where you
actually tried what you are talking about. It's not that much more typing.
</grr>
I'm hungry. I don't know why I'm putting off food even a minute to do this ;-/

Regards,
Bengt Richter
Jul 18 '05 #2

P: n/a
Hi.
I've made some small changes to your code. It appears to be doing what
you're looking for now.
HTH,
Sean
class BackupSet:
fileGroupList = []

class FileGroup:
sourceDir = ''
destinDir = ''
def __init__(self):
self.sourceDir = r'c:\folder' # use a raw string or escape the
'\'
self.destinDir = r'd:\folder' # like so, 'c:\\folder'

fileGroup = FileGroup()

# backupSet = BackUpSet() <-- NameError!
backupSet = BackupSet()

backupSet.fileGroupList.append(fileGroup)

# print bkset.fileGroupList[0].sourceDir <-- where did 'bkset' come from?
# using backupSet instead ....
print backupSet.fileGroupList[0].sourceDir

# outputs "c:\folder"

Jul 18 '05 #3

P: n/a
Thanks for the help both of you. Got that under control.
"python newbie" <me*******@hotmail.com> wrote in message news:pM*******************@newssvr25.news.prodigy. com...
Is there anything that would get in the way of me being able to reference an object attribute, from within a list.

I have:
# one class here

class BackupSet:
fileGroupList = []

# another class here

class FileGroup:
sourceDir = ''
destinDir = ''
def __init__(self):
self.sourceDir = 'c:\folder'
self.destinDir = 'd:\folder'
fileGroup = FileGroup()

backupSet = BackUpSet()

backupSet.fileGroupList.append(fileGroup)

# when i try to reference an attribute here, I just get 'none'

print bkset.fileGroupList[0].sourceDir or
print "%s" % bkset.fileGroupList[0].sourceDir


thanks
Steve
Jul 18 '05 #4

P: n/a
Sorry about that, I retyped it because I was actually using dom to import an
xml file, and the nodes were used to populate the object attributes, not
the literal strings I was using. I didn't want to put the xml code in this
post. I also set my email to text only, so no more html.
"Bengt Richter" <bo**@oz.net> wrote in message
news:bq**********@216.39.172.122...
On Sun, 30 Nov 2003 01:32:05 GMT, "python newbie" <me*******@hotmail.com> wrote:
This is a multi-part message in MIME format. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- So far, so annoying
[...]

Is there anything that would get in the way of me being able to =
reference an object attribute, from within a list.

I have:

Where do you have it? This is not an actual verbatim example of something

you tried.
# one class here

class BackupSet: ^--- this is different from your constructor call below at

[1]
fileGroupList =3D []
=20
# another class here

class FileGroup:
sourceDir =3D ''
destinDir =3D ''
def __init__(self):
self.sourceDir =3D 'c:\folder'
self.destinDir =3D 'd:\folder' =20
fileGroup =3D FileGroup()

backupSet =3D BackUpSet()

^----- [1] So I know you didn't do this.

backupSet.fileGroupList.append(fileGroup)

# when i try to reference an attribute here, I just get 'none'

Maybe you have some old code that reads
self.sourceDir = 'none'
in class FileGroup. Who can tell?

print bkset.fileGroupList[0].sourceDir or

^^^^^-- what the heck is that?
print "%s" % bkset.fileGroupList[0].sourceDir
=20

thanks
Steve
------=_NextPart_000_0025_01C3B69E.E2B92BA0
Content-Type: text/html;

<grr>
Two things:
1. Don't post HTML
2. Do post verbatim copy/pasted examples from the window where you
actually tried what you are talking about. It's not that much more

typing. </grr>
I'm hungry. I don't know why I'm putting off food even a minute to do this ;-/
Regards,
Bengt Richter

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.