Jump to content

Using python class as struct?

LightCode Gaming
Go to solution Solved by Azgoth 2,

There's actually no need for getter and setter functions in Python--that functionality is already built in to classes, so writing those functions in is completely unnecessary.  Using your class GROUPS,

foo = GROUPS(var1, var2)

# This does the exact same thing as foo.getGName():
foo.GName

# This does the exact same thing as foo.setGName(var3):
foo.GName = var3

Other object-oriented languages do use and require setter/getter functions, but Python is not one of them.  Writing them just duplicates functionality that's already there.  So your definition of GROUPS doesn't need any of the set/get functions:

class GROUPS:
    def __init__(self, gname, ips):
        self.GName = gname
        self.IPS = ips

So I'm making a program for one of my classes. I need to make something like a struct from C++ in python but I don't know python well enough to know how to do that.

I need to store:

  • Group name (only one name)
    • Get group name
    • Set group name
    • Search for group name to add/remove/find all IP's associated with said group
  • List of IP's
    • Be able to list off all IP's
    • Add/remove individual IP
    • Size of list (Use len()?)

GIGABYTE Z97MX-G516GB DDR3 | I5 4690k @ 4.4ghz | 1TB SSHD, 500GB HDD, 128GB SSD | GTX 1070 8GB | Corsair Graphite 230 | EVGA 650W | Hyper 212 EVO

 

Cinebench R15: 636(all cores), 127FPS

 

Link to comment
Share on other sites

Link to post
Share on other sites

class testclass:(object)
	def __init__(self, var1, var2):
		self.var1 = var1
		self.var2 = var2

	def getVar1(self):
		return self.var1

	def setVar1(self, var1)
		self.var1 = var1
	
	def getVar2(self):
		return self.var2
	
	def setVar2(self, var2)
		self.var2 = var2

Here's an example of using python classes with getters and setters, the rest I leave as an exercise to the reader

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, DJHenjin said:

class testclass:(object)
	def __init__(self, var1, var2):
		self.var1 = var1
		self.var2 = var2

	def getVar1(self):
		return self.var1

	def setVar1(self, var1)
		self.var1 = var1
	
	def getVar2(self):
		return self.var2
	
	def setVar2(self, var2)
		self.var2 = var2

 

 

So I must be doing something wrong... It keeps giving me errors. Here's the class I made:

class GROUPS:
    def __init__(self, GName, IPS):
        self.GName = GName
        self.IPS = IPS

    def getGName(self):
        return self.GName

    def setGName(self, GName):
        self.GName = GName
    
    def getIPS(self):
        return self.IPS
    
    def addIP(self, IPS):
        self.IPS.append(IPS)
        
    def remIP(self, IPS):
        self.IPS.remove(IPS)

And when I do this code....

def CREATEGROUP(client,group):
    newGroup = GROUPS(group,client[0])
    newGroup.setGName(newGroup,group)
    newGroup.addIP(newGroup,client[0])
    return newGroup

It says that I'm passing 3 args instead of 2? client[0] gives the IP using simple sockets.
Any idea what I'm doing wrong?

GIGABYTE Z97MX-G516GB DDR3 | I5 4690k @ 4.4ghz | 1TB SSHD, 500GB HDD, 128GB SSD | GTX 1070 8GB | Corsair Graphite 230 | EVGA 650W | Hyper 212 EVO

 

Cinebench R15: 636(all cores), 127FPS

 

Link to comment
Share on other sites

Link to post
Share on other sites

is CREATEGROUP it's own function outside of the class? remember you have to create an instance of the groups class before you can use it. like this:

<your group class goes here>


testgroup = GROUPS(group, clients)
testgroup.getGName()
testgroup.setGName("testgroup1")

Don't forget the supporting functions needed. If you like send the complete code in an attachment and I will run it to see what guidance I can offer on solving the issues

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, DJHenjin said:

is CREATEGROUP it's own function outside of the class? remember you have to create an instance of the groups class before you can use it. like this:


<your group class goes here>


testgroup = GROUPS(group, clients)
testgroup.getGName()
testgroup.setGName("testgroup1")

Don't forget the supporting functions needed. If you like send the complete code in an attachment and I will run it to see what guidance I can offer on solving the issues

 

Sent it in messages

 

GIGABYTE Z97MX-G516GB DDR3 | I5 4690k @ 4.4ghz | 1TB SSHD, 500GB HDD, 128GB SSD | GTX 1070 8GB | Corsair Graphite 230 | EVGA 650W | Hyper 212 EVO

 

Cinebench R15: 636(all cores), 127FPS

 

Link to comment
Share on other sites

Link to post
Share on other sites

newGroup.addIP(newGroup,client[0])

Your addIP and setGName functions only take 1 parameter but you're passing them 2.

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

newGroup.addIP(newGroup,client[0])

Your addIP function only takes 1 parameter but you're passing it 2.

 

 
def addIP(self, IPS):
        self.IPS.append(IPS)

No? 

GIGABYTE Z97MX-G516GB DDR3 | I5 4690k @ 4.4ghz | 1TB SSHD, 500GB HDD, 128GB SSD | GTX 1070 8GB | Corsair Graphite 230 | EVGA 650W | Hyper 212 EVO

 

Cinebench R15: 636(all cores), 127FPS

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, LightCode Gaming said:

def addIP(self, IPS):
        self.IPS.append(IPS)

No? 

self isn't a parameter, it's automatically set to the object before the dot.

So, newGroup.addIP(client[0]) actually calls addIP(newGroup, client[0]). Just like C++ but in C++ the "self" parameter is hidden.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

self isn't a parameter, it's automatically set to the object before the dot.

So, newGroup.addIP(client[0]) actually calls addIP(newGroup, client[0]). Just like C++ but in C++ the "self" parameter is hidden.

 

Cool. Didn't know that. Thanks!

GIGABYTE Z97MX-G516GB DDR3 | I5 4690k @ 4.4ghz | 1TB SSHD, 500GB HDD, 128GB SSD | GTX 1070 8GB | Corsair Graphite 230 | EVGA 650W | Hyper 212 EVO

 

Cinebench R15: 636(all cores), 127FPS

 

Link to comment
Share on other sites

Link to post
Share on other sites

There's actually no need for getter and setter functions in Python--that functionality is already built in to classes, so writing those functions in is completely unnecessary.  Using your class GROUPS,

foo = GROUPS(var1, var2)

# This does the exact same thing as foo.getGName():
foo.GName

# This does the exact same thing as foo.setGName(var3):
foo.GName = var3

Other object-oriented languages do use and require setter/getter functions, but Python is not one of them.  Writing them just duplicates functionality that's already there.  So your definition of GROUPS doesn't need any of the set/get functions:

class GROUPS:
    def __init__(self, gname, ips):
        self.GName = gname
        self.IPS = ips
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×