[英]function within a class in python
I am quite new to OOP in Python.我对 Python 中的 OOP 很陌生。
To avoid duplicate code in methods of a class I want to introduce a function within a class which is then called from the methods.为了避免 class 方法中的重复代码,我想在 class 中引入 function ,然后从方法中调用它。 From logical point of view these function should belong to the class and should not be a global function.从逻辑的角度来看,这些 function 应该属于 class 并且不应该是全局 function。
I have attached a small (nonsense) example.我附上了一个小(废话)示例。 But the example only runs when the function "SillyName" is placed outside of the class as global function.但该示例仅在 function“SillyName”作为全局 function 放置在 class 之外时运行。 But it should be part of the class.但它应该是 class 的一部分。 I am not sure how to do that, because when I place it inside, I am getting an error (NameError: name 'SillyName' is not defined).我不知道该怎么做,因为当我把它放在里面时,我收到一个错误(NameError: name 'SillyName' is not defined)。 Can you help?你能帮我吗?
import random
class SillyPerson:
def __init__(self, FirstName, LastName):
self.FirstName = SillyName(FirstName)
self.LastName = SillyName(LastName)
def __str__(self):
return (f"Name is {self.FirstName} {self.LastName}")
def SillyName (name):
""" returns a silly name if a None-Monthy-Python-Name is passed"""
if name in "John Cleese Eric Idle Michael Palin Terry":
return name
else:
return ''.join(random.sample(name,len(name)))
person1 = SillyPerson ("Michael","Moore")
person2 = SillyPerson ("Silvester","Stalone")
print (person1)
print (person2)
Methods inside a class need to have self as argument when getting written and a leading self. class 中的方法在写入时需要将self作为参数并以 self 作为前导。 when getting called.被叫时。
import random
class SillyPerson:
def __init__(self, FirstName, LastName):
self.FirstName = self.SillyName(FirstName)
self.LastName = self.SillyName(LastName)
def __str__(self):
return (f"Name is {self.FirstName} {self.LastName}")
def SillyName (self, name):
""" returns a silly name if a None-Monthy-Python-Name is passed"""
if name in "John Cleese Eric Idle Michael Palin Terry":
return name
else:
return ''.join(random.sample(name,len(name)))
person1 = SillyPerson ("Michael","Moore")
person2 = SillyPerson ("Silvester","Stalone")
print (person1)
print (person2)
Thank you for the quick answers!感谢您的快速回答!
With this information when I need a function within a class which is not using instances of the class I would then有了这些信息,当我需要 class 中的 function 时,它没有使用 class 的实例
Thank you also for the link in one of the comments to the article, where the background is explained:还感谢您在文章的评论之一中提供链接,其中解释了背景:
Meaning of @classmethod and @staticmethod for beginner? @classmethod 和 @staticmethod 对初学者的意义?
import random
class SillyPerson:
def __init__(self, FirstName, LastName):
self.FirstName = self.SillyName(FirstName)
self.LastName = self.SillyName(LastName)
def __str__(self):
return (f"Name is {self.FirstName} {self.LastName}")
@staticmethod
def SillyName (name):
""" return sillyname if a not Monthy Python name is passed"""
if name in "John Cleese Eric Idle Michael Palin Terry":
return name
else:
return ''.join(random.sample(name,len(name)))
person1 = SillyPerson ("Michael","Moore")
person2 = SillyPerson ("Silvester","Stalone")
print (person1)
print (person2)
import random
class SillyPerson:
def __init__(self, FirstName, LastName):
self.FirstName = self.SillyName(FirstName)
self.LastName = self.SillyName(LastName)
def __str__(self):
return (f"Name is {self.FirstName} {self.LastName}")
def SillyName (self,name):
""" returns a silly name if a None-Monthy-Python-Name is passed"""
if name in "John Cleese Eric Idle Michael Palin Terry":
return name
else:
return ''.join(random.sample(name,len(name)))
person1 = SillyPerson ("Michael","Moore")
person2 = SillyPerson ("Silvester","Stalone")
print (person1)
print (person2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.