osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Not Defined error in basic code


On 3/14/19 10:11 AM, Calvin Spealman wrote:
> Where are you seeing something like this? The two lines under `class 
> weapon:` are not correct because they are variable names that you've 
> never defined.
>
> Maybe you intended this to "declare" the attributes for the class? 
> That isn't something you need to do in Python. If you simply remove 
> these lines your example should work.
>
> On Thu, Mar 14, 2019 at 10:05 AM Jack Dangler <tdldev at gmail.com 
> <mailto:tdldev at gmail.com>> wrote:
>
>     Just getting started with tutorials and such, and don't understand
>     this -
>
>     <file: class_weapon.py>
>
>     class weapon:
>     ???? weaponId
>     ???? manufacturerName
>
>     ???? def printWeaponInfo(self):
>     ???????? infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
>     self.manufacturerName)
>     ???????? return infoString
>
>     <file: weaponTrack.py>
>
>     import class_weapon
>
>     MyWeapon=weapon()
>     MyWeapon.weaponId = 100
>     MyWeapon.manufacturerName = "Glock"
>
>     print(MyWeapon.printWeaponInfo)
>
>     executing 'python3 weaponTrack.py' results in this bailing on the
>     first
>     element in the class with "not defined". I've been staring at
>     templates
>     of this exact structure for about an hour trying to figure out why
>     this
>     isn't running at all. Is it simply because it isn't all in one file?
>     Thanks for any guidance. Really appreciate the help.
>
>
>     Thanks.
>
>     -- 
>     https://mail.python.org/mailman/listinfo/python-list
>
>
>
> -- 
>
> CALVIN SPEALMAN
>
> SENIOR QUALITY ENGINEER
>
> cspealma at redhat.com <mailto:cspealma at redhat.com> M: +1.336.210.5107 
> <tel:+1.336.210.5107>
>
> <https://red.ht/sig>
> TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>

Calvin

Thank you for the reply. I tried defining them in the form of 'int 
weaponId' but that didn't help. I finally put it in this form 
'weaponId=0" and it liked that. So, i updated the class file to be as 
follows -

<file: class_weapon.py>

class weapon:
 ???? weaponId=0
 ???? manufacturerName=""

 ???? def printWeaponInfo(self):
 ???????? infoString = "ID: %d Mfg: %s " % (self.weaponId, 
self.manufacturerName)
 ???????? return infoString

The second file now looks like this -

<file: weaponTrack.py>

import class_weapon
MyWeapon=class_weapon.weapon
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

so now, when I run 'python3 weaponTrack.py', I get <function 
weapon.printWeaponInfo at 0x7f2bd3ae7510>, but am expecting

ID: 100 Mfg: Glock ...