A class is more or less a fancy wrapper for a dict of attributes to objects. When you instantiate a class you can assign to its attributes, and those will be stored in foo.__dict__; likewise, you can look in foo.__dict__ for any attributes you have already written.
This means you can do some neat dynamic things like:
class Employee: pass
def foo(self): pass
Employee.foo = foo
as well as assigning to a particular instance. (EDIT: added self parameter)
A class is more or less a fancy wrapper for a dict of attributes to objects. When you instantiate a class you can assign to its attributes, and those will be stored in foo.__dict__; likewise, you can look in foo.__dict__ for any attributes you have already written.
This means you can do some neat dynamic things like:
class Employee: pass
def foo(self): pass
Employee.foo = foo
as well as assigning to a particular instance. (EDIT: added self parameter)
Try with lambda:
john.greet = lambda : print( 'hello world!' )
The you'll be able to do:
john.greet()
EDIT: Thanks Thomas K for the note - this works on Python 3.2 and not for Python2, where print appeared to be statement. But this will work for lambdas, without statements (right? Sorry, I know only python3.2 (: )
[Python] What's a empty object and user-defined method objects?
python - Why empty function are needed - Software Engineering Stack Exchange
Can you create a class with an empty list attribute
How can I create an empty list?
Videos
https://docs.python.org/3/tutorial/classes.html#class-objects
They say calling a class object creates a empty object, what's that?
https://docs.python.org/3/reference/datamodel.html
Under Callables, under Instance Methods
They were talking about user-defined methods objects are created if I get a attribute from a class (or instance of it) that's a user defined function or class method object.
But isn't MyClass.func a function and not a method
class Myclass: def func(self): pass print(Myclass.func) # function print(Myclass().func) #bounded method
So what's a user defined method object?
In shell languages of the Bourne family, the : command, which does nothing at all, is typically used in two situations:
Placeholder for when something expects a mandatory command, e.g.
while some_condtion do : donesince
dorequires at least one command.Discarding the arguments, but performing side effects inside the argument list, e.g.
: ${myvar=foo}
I'm sure there are probably other applications that shell experts would know :)
In Python (and other languages) it's used less frequently. It can act as an argument to a higher-order function when you don't actually want to do anything. For example, say you have a function that submits a form and allows an function to be called asynchronously after submission is complete:
def submit(callback=empty_func):
...
This way, if the callback is not provided, the submission will still go through but no further actions will be performed. If you had used None as the default value, you'd have to explicitly check whether the callback was None, adding clutter to the code.
Rufflewind did a good job covering when you might use an empty function in a completed program. In my experience, it tends to be used more often in unfinished programs, so you can plan out what you're going to write and have it still compile until you get around to actually implementing it. In other words, it's usually just a placeholder.
It's necessary in python's case because it uses indentation to mark blocks, unlike C-like languages who use braces {}. This means if you didn't have pass, the parser couldn't tell if you meant to leave it empty or if you forgot. Including pass makes the parser much simpler, and gives you a convenient word to search for when you're looking for unimplemented blocks.
I'm trying to create texas holdem with players being class instances with hand as an attribute of type list. When I instantiate a class with no value for hand it says I need instantiate it with a list, and when I try to use self.hand = None it doesn't allow me to append new generated cards to it since it's value type none, what should I do to work around this?
Edit: Now that I'm home I can add my code for people to see
Main Code
from pokerMethods import *
from playerClass import *
def main():
deckNumber = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
deckSuit = ["Spades", "Diamonds", "Clubs", "Hearts"]
cardList = []
playerList = []
p1 = Player("Herp", "Derp")
p2 = Player("Herpy", "Derpy")
createCard(p1, deckNumber, deckSuit, cardList)
createCard(p2, deckNumber, deckSuit, cardList)
print(p1.hand)
print(p2.hand)
main()Class Code:
class Player:
def __init__(self, fname: str, lname: str, hand = [], money = 0):
self.fname = fname
self.lname = lname
self.hand = hand
self.money = money
def addCard(self, newCard):
self.hand.insert(0,newCard)
def removeMoney(self, bet):
self.money -= bet
def addMoney(self, bet):
self.money += betMethod Code
import random
def createCard(player, deckNumber, deckSuit, cardList,):
cardPlayer = deckNumber[random.randint(0, len(deckNumber)-1)] + " " +
deckSuit[random.randint(0, len(deckSuit)-1)]
if cardPlayer not in cardList:
cardList.append(cardPlayer)
player.addCard(cardPlayer)added hand = [] and then did self.hand = hand because when I did just self.hand = [] it gave me the error
Traceback (most recent call last):
File "Pythons Test Shit\Test'.py", line 17, in <module>
main()
File "Pythons Test Shit\Test'.py", line 9, in main
p1 = Player("Herp", "Derp")
TypeError: __init__() missing 1 required positional argument: 'hand'
and with hand = [] in the initializer
Player 1: ['2 Hearts', '1 Clubs']
Player 2: ['2 Hearts', '1 Clubs']
both instances are having their lists edited
edit 2:
re-read the comments and saw u/Binary101010 's comment, sorry for not trying that before doing all my edits and stuff but thank you so much, it worked