Hi! im trying to create an object in python, but when i try to call the length of that object, it says "Mp3tag instance has no attribute '__len__'"

i know i can define a method on the "object"(i dont know if im creating the object in the right way) called __len__, but what code should i put inside?

mp3tag.py

class Mp3tag:

    error = ""
    date = ""
    trackNumber = ""
    artist = ""
    title = ""
    album = ""
    genre = ""
    numFiles = ""

maybe im creating the object in a wrong way, i googled over the internet but i never found something that explains how to create the objects

tnx and sorry about my poor english

Recommended Answers

All 11 Replies

Why would you even want the length of Mp3tag? If you really want to do this, you know the length so just return the number in __len__.

its because i have an if that asks this

if(len(mp3tag) > 0):
[INDENT]show an error report[/INDENT]

but when i try to ask the length it "AttributeError: Mp3tag instance has no attribute '__len__'", maybe there is a best way to do this, but to my primitive programing :P i dont have a better idea

Why would you do that? Might help if you just explained what you are trying to accomplish with this. Why would that show an error report?

What exactly do you want returned by the __len__ function? The length of the genre string, the title string, album string, etc? Or do you want it to return something to do with some of those strings being blank?

Also, as far as I know, len() is for strings, but I could be off about that. Like len('Hello World') returns 11.

P.S. you should restructure your Mp3tag class I think. Just like:

class Mp3tag:
    def __init__( self ):
        self.error = ""
        self.date = ""
        self.trackNumber = ""
        self.artist = ""
        self.title = ""
        self.album = ""
        self.genre = ""
        self.numFiles = ""

Whenever you initialize a Mp3tag object, the function __init__ gets called. Self just refers to the class itself, so in other functions within the class, you can access the variable title by writing self.title .

im trying to insert every error on the object, the app, scans for mp3 that have tag, if for some reason, the tag its empty or it have ascii characters it raises an excetption that insert the information of the path, artist, etc into the object mp3tag.
for later uses like editing the mp3 that have errors, and a little resume about the scan.
im thinking something like the object.size on c#
now that i read what u say... len is only for strings... and i understand that i need a list...
but i will wait to the answer of the ppl who know
and tnx shadwickman, ill change the class right now

So, your explanation is really difficult to follow, but I think you mean something about raising an error if it finds a blank tag in the Mp3tag object? Like an instance of it having a blank 'title' string? If that's the case then you could just write a function to check each of those tags and then if it detects one as blank, execute the appropriate code you want for dealing with it.
Sorry if I'm way off with what you're actually wanting to do. I've reread your description/explanation of it, but I'm having trouble understanding precisely what you want; it seems a bit vague to me. Sorry if I couldn't be of help.

Just for the record len() isn't only for strings but I still don't see how it would be helpful with what you're doing.

I think what you're trying to do is locate un-tagged or mis-tagged MP3s but I'm not sure. How are you determining these two cases?

My understanding is that you want to use Mp3tag to report errors yet from your original post I get the impression that you are trying to use Mp3tag to locate errors.

its really ok, you helped me more than you think.
its because my writed english is poor.
when you pointed that len() was only for strings i solved the problem.

sorry adam, i didnt see your reply.
if i can see the size of the object i can do

if(len(mp3tag) > 0):

    show an error report

and also execute the code to show the modals showing the reports, etc
because if i have 1 or more errors inside that object the code will execute

its really ok, you helped me more than you think.
its because my writed english is poor.
when you pointed that len() was only for strings i solved the problem.

Like I said before, it's *not* only for strings but the way you are trying to use it is probably not a good idea.

Something like this seems more reasonable, if I understand what you are doing: if(mp3tag.error is not ""):

it worked flawlessly.
many tnx to evryone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.