그래서 저는 TV 프로그램 제목을 수정하는 python 스크립트를 작성하고 있습니다. 이것은 무엇보다 연습을위한 것입니다. @property 데코레이터를 사용하려고하는데 var.property를 할 때 var이 속성에 전달 될 것이라고 생각했기 때문에 splitName()을 얻으려고하는 것입니다. 기본 클래스에서 hasDots 속성을 사용할 수있게하려면 어떻게 이름을 Var 속성에 전달할 지 모르겠습니다. 방법으로이 작업을 수행 할 수 있음을 알고 있지만 속성을 사용하는 방법을 배우려고합니다. splitName() 메서드는 일단 기본 클래스를 제대로 가져 오면 기본 메서드가됩니다.어떻게 파이썬에서 @property에 값을 전달합니까?
이 문제에 대한 도움을 주시면 감사하겠습니다. 또한 저는 파이썬에 대해 아주 새로운 것입니다. 그래서 "unpythonic"이라면 무엇이든지하고 있습니다.
exceptibleExts = ['.avi','.mkv','mp4']
class Base(object):
def __init__(self, source):
self.source = source
self.isTvShow()
# this method will return the file name
def fileName(self):
for name in self.source:
name, ext = os.path.splitext(name)
yield name, ext
@property
def isTvShow(self):
names = self.fileName()
for name in names:
if str(name[1]) in exceptibleExts and str(name[1]).startswith('.') == False:
return True
else:
return False
@property
def hasDots(self):
names = self.fileName()
for name in names:
if '.' in str(name[0]):
return True
else:
return False
@property
def hasDashes(self):
names = self.fileName()
for name in names:
if '-' in str(name[0]):
return True
else:
return False
@property
def startswithNumber(self):
names = self.fileName()
for name in names:
if str(name[0].lstrip()[0].isdigit()):
return True
else:
return False
@property
def hasUnderscore(self):
names = self.fileName()
for name in names:
if '_' in str(name[0]):
return True
else:
return False
class names(Base):
def __init__(self, source):
self.source = source
#pass
self.splitName()
#this method returns true if the show title is in the file name... if not it will return false
def hasShowTitle(self):
pass
def splitName(self):
#names = self.fileNames
showInfo = {}
for name in self.fileName():
print name.hasDots
? 코드가 원하지 않는 것은 무엇입니까? – BrenBarn
기본 클래스에서 hasDots 속성을 사용할 수 있도록 splitName()을 가져 오려고했지만 Var 이름을 속성에 전달하는 방법을 모르겠습니다. 방법으로이 작업을 수행 할 수 있음을 알고 있지만 속성을 사용하는 방법을 배우려고합니다. –
처음에는 절차 적 스타일로 코드를 작성하십시오. 지금 당신의 코드는 추악하고 당신이하고 싶은 것을 이해하기가 어렵습니다. 예를 들어 "for name in self.fileName()"name은 hasDots 메소드가없는 문자열이며 camelCase를 사용하면 phythonic이 아닙니다. 기초부터 시작해 봅니다. – Denis