On 3/14/19 12:50 PM, Calvin Spealman wrote:
You still need to get of the two lines at the start of your class,
they are unnecessary and reference variables you never defined:
class weapon:
weaponId # Get rid of this line!
manufacturerName # And this one, too!
On Thu, Mar 14, 2019 at 12:43 PM Jack Dangler <tdl...@gmail.com
<mailto:tdl...@gmail.com>> wrote:
On 3/14/19 10:39 AM, Calvin Spealman wrote:
A few notes, Jack:
On Thu, Mar 14, 2019 at 10:32 AM Jack Dangler <tdl...@gmail.com
<mailto:tdl...@gmail.com>> wrote:
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
<tdl...@gmail.com <mailto:tdl...@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
cspea...@redhat.com <mailto:cspea...@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=""
Technically this will work, but it won't always work. You're
assigning these values directly to the class (or type) and not to
individual objects of that type.
This will break very badly if you try to do this with any type of
value that can be changed (like a list, which you can add things
to) because you'll accidentally
modify values shared between ALL objects of the same type.
Instead, you want to define a __init__ method, which is called
when all objects of this type are
created, and assign the attributes in there. Like this:
def __init__(self):
self.weaponId = 0
self.manufacturerName = ""
Of course, you could make it easier to create the specific
objects you want by passing parameters at the creation of the object:
def __init__(self, weaponId, manufacturerName):
self.weaponId = weaponId
self.manufacturerName = 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"
If you follow my advice above, you won't need to override the
values here.
But you aren't actually creating anything here, because this line:
MyWeapon = class_weapon.weapon
Doesn't create anything. It just assigns the class you made to a
new name. What you probably meant to do, and can do with the
__init__ I suggest above, is create an instance of your weapon
class like this:
MyWeapon = class_weapon.weapon(100, "Glock")
print(MyWeapon.printWeaponInfo)
Similarly, you probably meant to call this method but without
parenthesis on the method you just printed the object representing
the method itself, rather than calling it and printing the value
it returns.
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 ...
I hope this helps.
--
CALVIN SPEALMAN
SENIOR QUALITY ENGINEER
cspea...@redhat.com <mailto:cspea...@redhat.com> M:
+1.336.210.5107 <tel:+1.336.210.5107>
<https://red.ht/sig>
TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>
Calvin
Really great explanation! Giving me a lot to go on. I changed the
files as suggested (I think), and now have this -
The first file now looks like this -
<file: class_weapon.py>
class weapon:
weaponId
manufacturerName
# Creation/Instantiation
def __init__(self, weaponId, manufacturerName):
self.weaponId = weaponId
self.manufacturerName = manufacturerName
# Print the class data
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(100, "Glock")
#MyWeapon.weaponId = 100
#MyWeapon.manufacturerName = "Glock"
print(MyWeapon.printWeaponInfo())
Results:
python3 ./weaponTest.py
Traceback (most recent call last):
File "./weaponTest.py", line 1, in <module>
import class_weapon
File "/home/jack/Py/weaponTrack/class_weapon.py", line 1, in
<module>
class weapon:
File "/home/jack/MyScripts/Py/weaponTrack/class_weapon.py", line
2, in weapon
weaponId
NameError: name 'weaponId' is not defined
--
CALVIN SPEALMAN
SENIOR QUALITY ENGINEER
cspea...@redhat.com <mailto:cspea...@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 very much for your patience! so the elements in the class are
both declared and defined using the def __init__(self... construct. A
little foreign to me, but I get it! And it does lend itself to being a
more portable framework for working with classes. Thank you once again.
Now I can keep reading and understand a whole lot more !
Thanks, again!
--
https://mail.python.org/mailman/listinfo/python-list